I have a ListView filled with checkboxes, I want to change the checkbox status from a button click this is my code.
import QtQuick
import QtQuick.Controls 2.15
Window {
width: 640
height: 480
visible: true
title: qsTr("Hello World")
ListView {
id: mylist
width: parent.width
height: 300
anchors {
top: parent.top
left: parent.left
right: parent.right
bottom: change_state.top
bottomMargin: 20
}
model: ListModel {
id: mymodel
ListElement {
mystatus : false
}
ListElement {
mystatus : false
}
ListElement {
mystatus : false
}
}
delegate: Rectangle {
height: 50
width: parent.width
CheckBox {
id: cb
checked: mystatus
anchors.fill: parent
text: "State"
}
}
}
Button {
id: change_state
anchors {
bottom: parent.bottom
}
text: "Change status"
onClicked: {
mymodel.setProperty(1, "mystatus", true)
console.log("PRESSED")
}
}
}
The problem is when I press the button after running the app it changes the status and checks the box, but if I remove the check manually then press the button again it has 0 effects, never been able to check it again.
QT 6.3 Windows 10 same issue for Android, IOS, and Desktop.
Tested with QT 5.15.2 and the same issue.
CodePudding user response:
When you click on the checkbox itself, you break the binding. Initially the checked
property is bound to mystatus
. But when you check the box manually, you're forcing checked
to be true or false, regardless of the state of mystatus
.
You can keep the binding intact by catching the onClicked signal and updating mystatus
, rather than forcing the checked
value to change.
CheckBox {
id: cb
checked: mystatus
anchors.fill: parent
text: "State"
onClicked: {
mystatus = !mystatus
}
}
UPDATE:
For your added question of unchecking all the boxes, you should do the same thing as above and update the mystatus value in the model rather than directly touching the checked
property so that the binding doesn't get broken.
for (var i = 0; i < mymodel.count; i) {
mymodel.setProperty(i, "mystatus", false);
}