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 undefined

    Does anybody know what to do?
    If i not create the delegate dynamically, then it is working perfect, but this is not an option.


  • Moderators

    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 use id of DelegateModel.



  • 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


  • Moderators

    @Mephi Hmm, sorry for that. I wasn't aware of it as i have never used it before. So digging in it seems that the attached properties of DelegateModel doesn't seem to work if the delegate is in separate file. Could be a bug I suppose. You can try reporting it here.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.