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
The code at http://comments.gmane.org/gmane.comp.lib.qt.qml/1778 should work, although I do see errors if the property is named 'data'; it looks like it is overriding some existing built-in property. Renaming it to 'myData' seems to work:
ListView {
id: myId
model: myModel
delegate: Item {
property variant myData: model
Text {
text: model.text
}
Text {
text: model.moreText
}
}
}
Text { text: myId.currentItem.myData.text }
(The myId.currentItem.text code in the original post didn't work because this was trying to refer to a text property within your delegate, which didn't exist.)
In regards to referring to model vs modelData within the delegate, the difference depends on the type of the model, rather than the number of roles in the model. If the model is a string list or object list, modelData is used to refer to the individual string or object from within a delegate (since string lists and object lists do not have any roles). For all other models, including the QML ListModel and the Qt C++ QAbstractItemModel, model.role can be used to refer to a role within a delegate.
You could alternatively access the model directly, with something like
Text { text: myModel[myId.currentIndex].text }
You can access a ListElement of ListModel using get() function.
Text { text: myModel.get(myId.currentIndex).text }
来源:https://stackoverflow.com/questions/5231882/how-do-you-access-the-roles-of-the-currentitem-from-a-listview-in-qml