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. QML AbstractItemModel access by Row and Role

QML AbstractItemModel access by Row and Role

Scheduled Pinned Locked Moved QML and Qt Quick
qabstractitemmorole
4 Posts 3 Posters 7.5k Views
  • 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.
  • R Offline
    R Offline
    Richard15
    wrote on 3 Jul 2015, 11:40 last edited by Richard15 7 Mar 2015, 12:43
    #1

    I have a C++ model based on QAbstractItemModel

    I need to access the data from QML JavaScript.
    I know the Parent ModelIndex, Row Number and the Role Name of the data I wish to access.

    How do I access the actual data using Javascript within QML?

    Every hint I can find involves rewriting the model to add either specific invokable getters, or an invokable get() method that returns an object representing the entire row. This feels completely wrong in every way, and cannot be the only option.

    Delegates clearly do this. How do they do this?

    T P 2 Replies Last reply 3 Jul 2015, 15:32
    0
    • R Richard15
      3 Jul 2015, 11:40

      I have a C++ model based on QAbstractItemModel

      I need to access the data from QML JavaScript.
      I know the Parent ModelIndex, Row Number and the Role Name of the data I wish to access.

      How do I access the actual data using Javascript within QML?

      Every hint I can find involves rewriting the model to add either specific invokable getters, or an invokable get() method that returns an object representing the entire row. This feels completely wrong in every way, and cannot be the only option.

      Delegates clearly do this. How do they do this?

      T Offline
      T Offline
      t3685
      wrote on 3 Jul 2015, 15:32 last edited by
      #2

      @Richard15

      The ListView is a cpp class which knows how to get the data from the model. No javascript involved. If you want to get data from the model in javascript you need to expose it yourself.

      Have a look in <QtInstall>\Src\qtquick1\src\declarative\graphicsitems

      1 Reply Last reply
      0
      • R Richard15
        3 Jul 2015, 11:40

        I have a C++ model based on QAbstractItemModel

        I need to access the data from QML JavaScript.
        I know the Parent ModelIndex, Row Number and the Role Name of the data I wish to access.

        How do I access the actual data using Javascript within QML?

        Every hint I can find involves rewriting the model to add either specific invokable getters, or an invokable get() method that returns an object representing the entire row. This feels completely wrong in every way, and cannot be the only option.

        Delegates clearly do this. How do they do this?

        P Offline
        P Offline
        p3c0
        Moderators
        wrote on 4 Jul 2015, 11:32 last edited by p3c0 7 Apr 2015, 11:33
        #3

        Hi @Richard15,
        You can do something like this explained here.
        Also starting from Qt 5.5 following works somehow (since I don't know the reason yet).
        Assuming myModel is a C++ QAbstractItemModel derived model set as a context property, then in QML :

        console.log(myModel.data(myModel.index(0,0),258))
        //where
        //(0,0) = row and column
        //258 = the role enum number, you can get these from C++
        

        This works outside of ListView too.
        Now trying to find out how to get the role outside ListView

        157

        1 Reply Last reply
        0
        • R Offline
          R Offline
          Richard15
          wrote on 6 Jul 2015, 13:07 last edited by Richard15 7 Jun 2015, 13:08
          #4

          @t3685
          As stated in the OP, I know I can write specific getter/setter methods into my QAbstractItemModel subclasses.
          However, if that really was the only option then QML would be effectively useless for nontrivial data-driven applications as the data models cease to be a standard interface.

          @p3c0
          Thanks, I hadn't thought of doing it that way.
          In Qt 5.4 you can do this if you set Q_INVOKABLE on QAbstractItemModel::index(int row, int column, const QModelIndex &parent) and QAbstractItemModel::data(const QModelIndex &index, int role) methods.
          I'm guessing this has been done for you in Qt 5.5.

          I have also found that in Qt 5.4 (and presumably 5.5) it's also possible to get there via a DelegateModel (often called a VisualDataModel in the documentation), as the DelegateModelGroup within that has a get(row) function that returns an Object through which you can access the data in the same way as in a QML Delegate.
          http://doc.qt.io/qt-5.4/qml-qtqml-models-delegatemodelgroup.html#get-method
          However, I haven't yet figured out when it reads the data from the underlying model.

          Thus, if the DelegateModel has only the default DelegateModelGroup:

          myDelegateModel.items.get(%row%).model.%role_name%
          

          In this, myDelegateModel is a DelegateModel object (which may also be used as the model in a ListView, GridView or PathView), %row% is the row number and %role_name% is the role.
          Set myDelegateModel.rootIndex to the appropriate parent QModelIndex.

          PS: $Deity the documentation is beyond awful. So many links straight back to exactly where you are masquerading as a link to what the thing is.

          1 Reply Last reply
          1

          1/4

          3 Jul 2015, 11:40

          • Login

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