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] Accessing arbitrary model data in QML
QtWS25 Last Chance

[Solved] Accessing arbitrary model data in QML

Scheduled Pinned Locked Moved QML and Qt Quick
5 Posts 3 Posters 4.9k 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.
  • M Offline
    M Offline
    mlong
    wrote on last edited by
    #1

    In QML, if you have a ListModel, you can arbitrarily fetch values by using modelname.get(index).item.

    Is there a comparable accessor method for data which is provided via an exposed C++ QAbstractListModel? I've looked all over for any documentation that might point me in the right direction, but haven't had any luck. It seems like this shouldn't be that difficult of a task.

    I know how to access the data through a ListView, or GridView, etc., but currently I have a fixed component that just needs to pull certain items from one arbitrary index at a time (i.e., a "page" that contains all of my custom roles from one row in the model.)

    Software Engineer
    My views and opinions do not necessarily reflect those of anyone -- living or dead, real or fictional -- in this universe or any other similar multiverse node. Void where prohibited. Your mileage may vary. Caveat emptor.

    1 Reply Last reply
    0
    • frankcyblogic.deF Offline
      frankcyblogic.deF Offline
      frankcyblogic.de
      wrote on last edited by
      #2

      QAbstractItemModel::data() in combination with QAabstractItemModel::index() ? You are looking for smth. else?

      1 Reply Last reply
      0
      • M Offline
        M Offline
        mlong
        wrote on last edited by
        #3

        That seems like a reasonable set of methods to use, I'm just not sure of what syntax I'd use inside of QML to make that happen.

        Software Engineer
        My views and opinions do not necessarily reflect those of anyone -- living or dead, real or fictional -- in this universe or any other similar multiverse node. Void where prohibited. Your mileage may vary. Caveat emptor.

        1 Reply Last reply
        0
        • C Offline
          C Offline
          conny
          wrote on last edited by
          #4

          You could simply add a method to your QAbstractModel. Something like:
          @ Q_INVOCABLE MyType* get(int index); @

          Then it's easy to access in QML.

          1 Reply Last reply
          0
          • M Offline
            M Offline
            mlong
            wrote on last edited by
            #5

            It turns out that what I was needing to accomplish was actually easily done by using a ListView.

            Since what I needed to do was basically have a page of data which updated a number of items based on a given index into a data model, I simply set up a full-page ListView with a delegate widget that was also page-sized. By setting the "interactive" property to false, I could prevent the user from swiping from page to page, and by using the positionViewAtIndex() method, I could immediately jump to the page that contained the appropriate data without the scrolling which would occur by just setting the currentIndex property.

            Funny how sometimes just putting something aside long enough will give you a fresh perspective on how things can work.

            Software Engineer
            My views and opinions do not necessarily reflect those of anyone -- living or dead, real or fictional -- in this universe or any other similar multiverse node. Void where prohibited. Your mileage may vary. Caveat emptor.

            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