I'm trying to access a role from a ListView in QML. Essentially, I have this in my QML:
ListView {
id: myId
model: myModel
delegate: Item {
Text {
text: model.text
}
Text {
text: model.moreText
}
}
}
myModel
is a QAbstractListModel implementation. The QML portion of this is a reusable component, so the model could have any number of different roles with various data types. What I would like to do is bind to the value of a given role of the currentItem
property of the ListView. In other words, I'd like to have some other Component
on the page that could bind a property to the currently selected item in the ListView as follows:
Text {
text: myId.currentItem.text // Or myId.currentItem.model.text (or something similar)
}
Please keep in mind that I need this generically available, as I'll be doing this a lot for a number of model types and I'm trying not to write that kind of custom code for each model and ListView.
It seems like it should be simple to access a property of the currently selected item, but as far as I can tell it is not possible. The problem is complicated further by the fact that models appear to be treated differently when there is only one role. By this I mean that sometimes you access your roles via model.roleName
whereas when there is only one role you use modelData
.
If anybody has any suggestions, I would truly appreciate it. Thanks so much!
EDIT
I found this:
http://comments.gmane.org/gmane.comp.lib.qt.qml/1778
However, this doesn't appear to work for me. I'm getting type errors when I try to use the data in my QML scripts, and there is no type casting available so I'm not sure what to do. Any suggestions are welcome!
Thanks!
Jack