[Solved] Accessing arbitrary model data in QML
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.)
QAbstractItemModel::data() in combination with QAabstractItemModel::index() ? You are looking for smth. else?
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.
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.
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.