Solved Retrieve number of elements of a QAbstractListModel and access items by index and role in qml
-
Hi,
If I expose a QAbstractListModel to qml, is there a way I can know the number of elements in the model? Or do I need to make my own Q_INVOKABLE function which gives me the number?
-
@maxwell31 Hi,
I guess you can use QAbstractItemModel::rowCount.
Most of QAbstractItemModel's methods can be invoked from QML now.
As the note says:Note: This function can be invoked via the meta-object system and from QML. See Q_INVOKABLE.
-
Thanks! I did not know I can simply call them from qml.
Another question: How can I access an item in the model via its role number? would itemData(index(row,0)) work or is there a simpler solution?
-
@maxwell31 you can use
data
andindex
I guess something like that :
myModel.data(myModel.index(row, 0), Qt.DispalyRole)
-
Thanks again. The way with calling it with data and index is working. I am wondering though whether there is an easier way, which would use the roleNames in my model. Otherwise, I need to register my model class, so that I have the roleEnums available.
-
@maxwell31 Usually you use a model with a listview, a repeater, or any other "item" view. Then you access your roles using names defined in your implementation of QAbstractItemModel::roleNames, not the enum values.
-
@maxwell31 What's is your goal exactly ?
-
I understand what you are saying. My problem is the following:
I have some c++ objects, which e.g. have a height property. I want to display these objects in my own qml bar chart. In order to compute the maximum height I iterate over the elements in my model in qml, and would like to access the height property. In a repeater or listmodel, the string role name is only available for the current object right? How could I access the height role inside a loop over my objects?
-
@maxwell31 You can browse them using a loop from QML. But yes you need to register your type in this case to access the enum values.
for(var i = 0; i < model.rowCount(); i++){
model.data(model.index(i, 0),
}I would suggest that you implement this kind of calculation in the C++ side, and just add a method like ``getMaxHeight()" in your model, with Q_INVOKABLE directive. At my opinion, it is easier to write and more efficient
-
Most likely simply going to do
qmlRegisterType<MyModel>("MyModel",1,0,"MyModel");
is probably the easiest. Though annoying, as it has to be imported in the qml file. Obviously a maybe better option would be to move more of the logic to c++.
But I understand it right, that there is no way in QML to iterate over the elements in a model and use the string role name?
-
@maxwell31 said in Retrieve number of elements of a QAbstractListModel and access items by index and role in qml:
But I understand it right, that there is no way in QML to iterate over the elements in a model and use the string role name?
I would say there is not simple way. But you could retrieve the roleNames() map from QML and then resolve role ID into names.
-
I think the roleNames map (QHash) does not convert to a type qml understands.
-
@maxwell31 Yes, as I said, there is no simple way. What about adding a methodQString roleName(int role)
in the model which return the name of a role.@maxwell31 Yes, as I said, there is no simple way. What about adding a method
int nameRole(QString roleName)
in the model which return the role according to the name.Anyway what does it changes if you use role or name of role ?
-
I've created such class as proof of concept : https://github.com/oKcerG/QmlModelHelper (with the WiP readme : https://gist.github.com/oKcerG/eeea734bdacc51b3ae58650de5f05943)
in qml you could then do :
yourModel.ModelHelper.roleForName("yourRoleName")
-
@Gojir4 said in Retrieve number of elements of a QAbstractListModel and access items by index and role in qml:
Anyway what does it changes if you use role or name of role ?
I usually don't know the integer values of my custom roles, and without registering the model (which is a practicable solution) I don't have access to them. But you are right, adding a nameRole function would work fine.
-
@GrecKo said in Retrieve number of elements of a QAbstractListModel and access items by index and role in qml:
I've created such class as proof of concept
Thanks for sharing