Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct
Access data of C++ model from QML?
From the C++ side of things my QML UI is passed a model
(QAbstractTableModel instance), used for a TableView. This works very
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  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?
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
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?!
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
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@
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++)
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.