Solved How to access C++ Model data directly from QML without any ListView/Repeater/etc ?
-
Hello everyone!
I am a beginner in Qt and QML. So far I am able to easily access my C++ Models (subclasses of QAbstractListModel) easily from QML via Controls like ListView, Repeater, ComboBox, etc.
However, I have reached a point where I would like to access data of my model via JavaScript in a QML function. Let's say, I would want to call thedata
method of my model directly from QML, and pass in the row information. How do i do that?Currently, I am having a
Q_INVOKABLE
method in my C++ code which accepts row number, and returns the appropriate value by invokingdata
within it.Is there a proper way to do this other than this? Initially I used
QList
instead of models, after reading up on QML Performance Optimisation guide, it looks like that is very performance heavy since it involves copying all at once when dealing with larger custom classes.Thank you, please excuse and correct if I said anything wrong.
-
What you are doing(Q_INVOKABLE) is the only way to access the model data
-
QAbstactItemModel::data
is already Q_INVOKABLE. -
@dheerendra @GrecKo Thank you, I did not know that.
-