How to add elements to a DelegateModelGroup depending on a property

前端 未结 2 881
无人共我
无人共我 2020-12-17 06:36

I have a ListModel and a DelegateModel, a ListView and 3 Buttons. The DelegateModel has one Group: myGroup With a click on of the first two buttons, I add elements to the Li

2条回答
  •  伪装坚强ぢ
    2020-12-17 06:58

    For those who are interested in it, with the hint of user2436719 I was able to hack together this model. It is a ListModel, and examplewise two DelegateModels, with groups to which the elements are added depending on a role of the listmodel.

    ListModel{
        id: rootModel
        onCountChanged: setGroups(count - 1)
        onDataChanged: setGroups(arguments[0].row)
        property DelegateModel sub1:
            DelegateModel {
                id: subModel1
                model: rootModel
                groups: [
                    DelegateModelGroup { name: 'myGroup' },
                    DelegateModelGroup { name: 'notMyGroup' }
                ]
                delegate: Rectangle {
                    width: model.width
                    height: model.height
                    color: model.color
                    border.width: 1
                }
                filterOnGroup: (root.toggle ? 'myGroup' : 'notMyGroup')
            }
        property DelegateModel sub2:
            DelegateModel {
                id: subModel2
                model: rootModel
                groups: [
                    DelegateModelGroup { name: 'myGroup' },
                    DelegateModelGroup { name: 'notMyGroup' }
                ]
                delegate: Rectangle {
                    radius: 5
                    width: model.width
                    height: model.height
                    color: model.color
                    border.width: 1
    
                    Text {
                        anchors.centerIn: parent
                        text: DelegateModel.groups.toString()
                    }
                }
                filterOnGroup: (root.toggle ? 'myGroup' : 'notMyGroup')
            }
    
        function setGroups(index) {
            console.log('set Groups for', index)
            var i = get(index)
            subModel1.items.setGroups(index, 1, ['items', (i.width === 80 ? 'myGroup' : 'notMyGroup')])
            subModel2.items.setGroups(index, 1, ['items', (i.width !== 80 ? 'myGroup' : 'notMyGroup')])
        }
    }
    

提交回复
热议问题