Model property set in listview: delegate can't find properties.
-
Hi all,
Let's say I have a window (rectangle) that has a widget (listview). When I do the following everything is working ( all names of contacts are print):
Window.qml:
ContactWidget { model: myModel }
}@
ContactWidget.qml:
@ListView {
delegate: Text { text: modelData.name }
}@When I set the model property within the ContactWidget it doesn't work:
Window.qml:
@Rectangle {
ContactWidget {}
}@ContactWidget.qml:
@ListView {
model: myModel
delegate: Text { text: myModel.name }
}@I get the following message:
Unable to assign [undefined] to QString text.
The model is a derived QAbstractListModel and the name property is a string...
-
How do you pass your myModel to qml?
-
It's a derived C++ class of QAbstractListModel. I pass it by:
setContextProperty("myModel",objectDerivedListModel);
-
Hi,
In the second case it looks like you are binding to myModel.name, which looks for a name property on the model itself (rather than for each individual row in the model). If modelData.name works for the first case, you should be able to use it for the second as well (rather than myModel.name).
If you haven't seen it already, the documentation for the specialized roles available within a model delegate are documented at http://doc.trolltech.com/4.7-snapshot/qdeclarativemodels.html .
Regards,
Michael -
Ah, yep, didn't notice myModel in second case. Changing it to modelData should help you.
-
That did the trick.. Very strange because I tried this before but then it didn't work.