ListView Model setProperty Work only 1 time
-
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, so what i did do wrong?
QT 6.3
Windows 10
same issue for Android, IOS, and Desktop.Update:
Tested with QT 5.15.2 and the same issue. -
per default the binding of the checkboxes checked property will break once you manually click the checkbox.
to preserve the binding, simply do sth. like this:CheckBox { id: cb checked: mystatus anchors.fill: parent text: "State" // in newer Qt versions this should be sufficient onClicked: { mystatus = checked } // otherwise additionaly use Connections or Qt.binding to preserve the binding }
-
In addition to what @lemons said, model property is not updated when you changed the check box from UI. Hence the issue. You can try with following piece of code inside the CheckBox.
onClicked: { _root.ListView.view.model.setProperty(1, "mystatus",checked) }