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. Access data of C++ model from QML?
QtWS25 Last Chance

Access data of C++ model from QML?

Scheduled Pinned Locked Moved QML and Qt Quick
6 Posts 3 Posters 4.1k 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.
  • E Offline
    E Offline
    enkore
    wrote on last edited by
    #1

    From the C++ side of things my QML UI is passed a model
    (QAbstractTableModel instance), used for a TableView. This works very
    nicely.

    Now, I want my QML UI to interact with selected rows, i.e. display data
    of the selected rows... for a start.

    I fail to see how this is possible. Clearly this has to bee done in the
    onSelectionChanged handler of the TableView, but how do I actually
    access the data?

    This answer [1] on qtcentre suggests adding a Q_INVOKABLE method to the
    model for this. However since the model implements already a
    standardized interface for querying data as well as metadata I don't
    think it's a good idea or particularly wise to implement additional
    interface extensions that are non-standard and also not generic.
    Sometimes it is also not possible or not feasible to modify the model.

    Now, somehow the data of course finds its way into QML, since I can see
    it in the TableView... however, the details of model access seem to be
    written in C++...

    So, long post, short recap: How do I access data of a C++ table model
    from within QML given the model and a row index?

    [1]
    http://www.qtcentre.org/threads/52850-How-to-get-data-from-TableView-QML-Desktop-Components?p=237157#post237157

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi and welcome to devnet,

      Did you saw this "chapter":http://qt-project.org/doc/qt-5/qtquick-modelviewsdata-cppmodels.html#qabstractitemmodel of Qt's documentation about that subject ?

      Hope it helps

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • E Offline
        E Offline
        enkore
        wrote on last edited by
        #3

        Hi,
        yes I found that one, but I didn't see the reference to DelegateModel the first time. I'm currently trying to work with that, but have to issues with it:

        • If I use the DelegateModel (with model-property set to my C++ model and the delegate property set to my item delegate) as the model for the TableView, the display is wrong (all items in the top left corner ; obviously some kind of layout issue)

        • However, dataModel.count and dataModel.items.count (dataModel is my DelegateModel instance) are correct.

        • dataModel.items.get(<someindex>) does even return an Object ; however it has no properties whatsoever (Object.keys(dataModel.items.get(<someindex>)) is empty). Trying to access properties "blindly" like <object from get()>.<rolename> didn't work (as expected).

        I'm not quite convinced that this is the way one should work with models... shouldn't that be a bit more... simple?

        I'm quite surprised that it's so hard to find an example of this ... does really no one ever need to access model data outside of a delegate?!

        1 Reply Last reply
        0
        • p3c0P Offline
          p3c0P Offline
          p3c0
          Moderators
          wrote on last edited by
          #4

          Hi,

          bq. does really no one ever need to access model data outside of a delegate?!

          If i understood you correctly you can access it by
          @
          tableViewId.model.get(tableViewIndex).role
          @

          157

          1 Reply Last reply
          0
          • E Offline
            E Offline
            enkore
            wrote on last edited by
            #5

            This seems only to work with QML-based models like ListModel. With a C++ model, I get an error:

            @TypeError: Property 'get' of object Logging::LogsModel(0x13343e0) is not a function@

            1 Reply Last reply
            0
            • p3c0P Offline
              p3c0P Offline
              p3c0
              Moderators
              wrote on last edited by
              #6

              Hmm, the best way would always be to go through the model i.e define a Q_INVOKABLE function as you mentioned earlier.

              Other pure QML way would be to interate over the childrens of TableView's contentItem.
              I had tested something similar but with ListView as follows:
              @
              for(var a=0; a<=listView.contentItem.children.length;a++)
              console.log(listView.contentItem.children[a].text)
              @

              This gives the texts of all elements of ListView even if the model is C++ based.
              May be you can try it for TableView but i have never tried this personally for TableView.

              157

              1 Reply Last reply
              0

              • Login

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