Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. [Solved]How to pass QModelIndex from QML to C++
Forum Updated to NodeBB v4.3 + New Features

[Solved]How to pass QModelIndex from QML to C++

Scheduled Pinned Locked Moved QML and Qt Quick
8 Posts 2 Posters 11.1k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    psbhardwaj09gmail.com
    wrote on 30 Jan 2013, 06:45 last edited by
    #1

    Hello,

    I have an C++ based application which exposed QAbstractmodel to QML file as data model.I want to get the Qmodelindex from list view. so that why i set c++ model as visualdata model & visual data model as listmodel as following
    @
    ListView
    {
    id: listView
    anchors.fill: parent
    model:VisualDataModel
    {
    model: cppModel //cppModel is model exposed from c++ file
    delegate: petDelegate
    }

     }
    

    @
    after that i want to get model index & pass it to c++. for that i write code as..
    @
    mainView.somesignal(listView.model.modelIndex(index))
    @
    but when i print modelindex's value it print as: QVariant(QModelIndex)

    So How can get the value of model index in QML??

    Edit: please wrap your code snippets with @ tags; Andre

    Pardeep Sharma

    1 Reply Last reply
    0
    • A Offline
      A Offline
      Adrien Leravat
      wrote on 30 Jan 2013, 08:37 last edited by
      #2

      If your problem is that the value you get is a QVariant instead of a QModelIndex, use the following on the C++ side to convert it back:

      @
      QModelIndex qmIndex = index.value<QModelIndex>();
      @

      Adeneo Embedded - www.adeneo-embedded.com

      1 Reply Last reply
      0
      • P Offline
        P Offline
        psbhardwaj09gmail.com
        wrote on 30 Jan 2013, 09:06 last edited by
        #3

        Actually i am using Christophe Dumez blog "(http://cdumez.blogspot.in/2010/11/how-to-use-c-list-model-in-qml.html)":http:// in my application. In qml i want that when i click on the list item, then it should return model index of that item so that i can take action based on that. in c++ code i capture this signal and based on modelindex i add/remove item in/from another list. for that in qml i write code as
        @
        signal blockUri(variant uri)//in mainview

        ListView
        {
        id: InfoView
        anchors.fill: parent
        model:VisualDataModel
        {
        model: watcherInfoModel
        delegate: petDelegate
        }
        }

        onClicked{
        mainView.blockUri(InfoView.model.modelIndex(index))//blockUri is signal & mainView is id of main view

        }
        @

        on c++ side i print value of of QVariant, is display as
        QVariant(QModelIndex, )

        and in QML it display as
        QVariant(QModelIndex)

        when i try to convert Qvariant inot Qmodelindex by using
        @QModelIndex qmIndex = index.value<QModelIndex>();@

        there is error like
        @c:\qtsdk\desktop\qt\4.8.1\msvc2010\include\qtcore\qmetatype.h:169: error: C2039: 'qt_metatype_id' : is not a member of 'QMetaTypeId<T>'
        with
        [
        T=QModelIndex
        ]@

        so how can get model index of item. Is there any other way to get modelindex??

        Pardeep Sharma

        1 Reply Last reply
        0
        • A Offline
          A Offline
          Adrien Leravat
          wrote on 30 Jan 2013, 09:24 last edited by
          #4

          On the c++ side, try adding
          @
          #include <QAbstractItemModel>
          @

          ... so the QModelIndex type is recognized.
          If it doesn't work try adding this too

          @
          Q_DECLARE_METATYPE(QModelIndex)
          @

          Adeneo Embedded - www.adeneo-embedded.com

          1 Reply Last reply
          0
          • P Offline
            P Offline
            psbhardwaj09gmail.com
            wrote on 30 Jan 2013, 10:54 last edited by
            #5

            Thank you. Now its working. i was missing to add @Q_DECLARE_METATYPE(QModelIndex)@

            Pardeep Sharma

            1 Reply Last reply
            0
            • A Offline
              A Offline
              Adrien Leravat
              wrote on 30 Jan 2013, 11:59 last edited by
              #6

              You're welcome ! Don't forget to mark your thread [SOLVED] :)

              Adeneo Embedded - www.adeneo-embedded.com

              1 Reply Last reply
              0
              • P Offline
                P Offline
                psbhardwaj09gmail.com
                wrote on 31 Jan 2013, 11:15 last edited by
                #7

                Actually i am new in this forum. So i don't know how to mark it as solved. Can you please tell me how to mark it as solved?

                Pardeep Sharma

                1 Reply Last reply
                0
                • A Offline
                  A Offline
                  Adrien Leravat
                  wrote on 31 Jan 2013, 12:33 last edited by
                  #8

                  Of course: Simply edit your first message (edit button near you avatar), and prepend [SOLVED] to the title of your thread. You can also add the "solved" tag to the thread tags (at the right of your first post), I did this.

                  Adeneo Embedded - www.adeneo-embedded.com

                  1 Reply Last reply
                  0

                  1/8

                  30 Jan 2013, 06:45

                  • Login

                  • Login or register to search.
                  1 out of 8
                  • First post
                    1/8
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • Users
                  • Groups
                  • Search
                  • Get Qt Extensions
                  • Unsolved