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 ListModel with different properties, that then will be used to add them or not add them to myGroup. The third button is used to toggle the filter.
ListModel {
id: rootModel
}
DelegateModel {
id: visualModel
model: rootModel
groups: [DelegateModelGroup { name: 'myGroup' }]
delegate: Rectangle {
width: model.width
height: model.height
color: model.color
border.width: 1
Component.onCompleted: DelegateModel.inMyGroup = Qt.binding(function() {return width == 80})
}
}
ListView {
width: 300
height: 480
model: visualModel
}
Rectangle {
id: b1
x: 350
width: 100
height: 50
color: 'lightsteelblue'
Text {
anchors.centerIn: parent
text: 'add 80'
}
MouseArea {
anchors.fill: parent
onClicked: rootModel.append( { width: 80, height: 30, color: 'steelblue' })
}
}
Rectangle {
id: b2
x: 350
y: 70
width: 100
height: 50
color: 'violet'
Text {
anchors.centerIn: parent
text: 'add 50'
}
MouseArea {
anchors.fill: parent
onClicked: rootModel.append( { width: 50, height: 30, color: 'violet' })
}
}
Rectangle {
id: b3
x: 350
y: 140
width: 100
height: 50
color: 'green'
Text {
anchors.centerIn: parent
text: 'filter'
}
MouseArea {
anchors.fill: parent
property bool toggle: false
onClicked: {
visualModel.filterOnGroup = toggle ? '' : 'myGroup'
toggle = !toggle
}
}
}
So far so good, as long as not filter is set, I can add elements of both types to the model, and then I can filter.
However I can't add elements to the model (Group) as long as I have set the filter. This is obvious, as per default the element is not added, so the component is not completed, and nothing is added to the group, until I unset the filter again, and all pending elements are instanciated, completed, and finally added.
Therefore I look for another way to automatically add the elements depending on a property to the group, or to exclude them.
I know, I could set includeByDefault: true
and then throw them out after they have been instanciated once. While this solves this particular issue, it does not solve the next, where I would change the property, therefore throw it out of the group, and then change it back. It won't reappear until the component is reinstanciated again.
Oh, and I know, I could solve this with C++, but I don't want to, unless necessary.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…