dynamic Delegate component and use of DelegateModelGroup in DelegateModel
-
Hello,
i have a simple test qmlproject, where i create a dynamically a delegate for a gridview, this delegate is used in a DelagetModel with groups, but in the delegate the groups are undefined :-(
here is a short example code:Rectangle { id: root anchors.fill: parent property var trackObjectViewDelegate: Qt.createComponent("DelegateItem.qml", root) DelegateModel { id: visualModel model: ListModel { ListElement { name: "blue" } ListElement { name: "green" } ListElement { name: "red" } } groups: [ DelegateModelGroup { name: "selected" } ] delegate: trackObjectViewDelegate } GridView { id: gridID anchors.fill: parent model: visualModel cellHeight: 300 cellWidth: 300 } }
in the DelegateItem.qml i try to read the selected property like this:
Rectangle { id: item height: 25 width: 200 Text { text: { var text = "Name: " + name if (item.DelegateModel.inSelected) text += " (" + item.DelegateModel.selectedIndex + ")" return text; } } MouseArea { anchors.fill: parent onClicked: item.DelegateModel.inSelected = !item.DelegateModel.inSelected } }
It is not working, and i get following error:
TypeError: Cannot read property 'inSelected' of undefinedDoes anybody know what to do?
If i not create the delegate dynamically, then it is working perfect, but this is not an option. -
Hi @Mephi and Welcome,
TypeError: Cannot read property 'inSelected' of undefined
The error says it exactly. The element is indefined and thus cannot access that property. Also I fail to see property named
inSelected
.You can either use
parent
or directly useid
ofDelegateModel
. -
Hello p3c0,
thanks for your reply, i know that the property
inSelected
is undefined, thats the intension of my question ;-) , but why it is undefined?it has to be defined, because it is working if i not create the delegate dynamically.
inSelected
property is generated automatically
http://doc.qt.io/qt-5/qml-qtqml-models-delegatemodel.html#groups-prop -