In QtQuick.Controls 1.4, there is a ComboBox, and in it there is a textRole property that sets the role that is extracted from the model for displaying in the drop-down list. Similar to this example, I created the dataRole property, but I don’t know how, having the text name of the role, extract the relevant data from the model. For example,
property string dataRole: "number" text: model.number //Так работает text: model.dataRole //А так не работает I tried using the data method:
text: model.data(index, roleNumber) but it requires a role number, not a textual title. I cannot get the number by textual name in QML, because the getRoleNames method getRoleNames not marked as Q_INVOKABLE .
How is this implemented with the textRole property?
UPD. Most likely, this code would work as it should, if the property method were marked as Q_INVOKABLE .
ComboBox { currentIndex: 2 model: ListModel { id: cbItems ListElement { text: "Banana"; color: "Yellow"; data: "1"} ListElement { text: "Apple"; color: "Green"; data: "2"} ListElement { text: "Coconut"; color: "Brown"; data: "3"} } textRole: "text" // Роль "text" используется внутри ComboBox для извлечения данных, отображаемых в выпадающем списке property string dataRole: "data" //Эту роль надо вывести в лог width: 200 onCurrentIndexChanged: console.debug(cbItems.get(currentIndex).text + ", " + cbItems.get(currentIndex).color + ", " + cbItems.get(currentIndex).property(dataRole)) } ps I'll get to the working computer only on Monday.
UPD2.
This is how it works, but it will be necessary to check it on the working draft, since QAbstractListModel used as a model there.
ComboBox { currentIndex: 2 model: ListModel { id: cbItems ListElement { text: "Banana"; color: "Yellow"; data: "1"} ListElement { text: "Apple"; color: "Green"; data: "2"} ListElement { text: "Coconut"; color: "Brown"; data: "3"} } textRole: "text" property string dataRole: "data" width: 200 onCurrentIndexChanged: { console.debug(cbItems.get(currentIndex)[dataRole]) } } UPD3. No, this is not the case with QAbstractListModel . There is no get method.
ChannelTypeModel { id: channelTypeModel } ComboBox { id: channelTypeSelector model: channelTypeModel textRole: "text" property string dataRole: "number" onCurrentIndexChanged: { console.log(model.data(model.index(currentIndex, 0), 257)) } }
dataRole = "number"), extract data from the model for this role. - maestro