I have a simple list of CheckBox
es, one for each day of the week. They depend on the value of days
, an integer using a mask, 1 bit for each CheckBox
.
Assigning to days
both with the "clear all" button or the "set all" button works and they update. However, once any of the boxes have been clicked, they no longer respond to changes in the dependent property days
.
Why is this? Are they somehow becoming unbound. If so, should i be manually re-binding them, and if so why?
Here's the code,
import QtQuick 2.7
import QtQuick.Controls 1.4
import QtQuick.Layouts 1.3
ApplicationWindow
{
visible: true
width: 800
height: 400
property int days: 0
ColumnLayout
{
Repeater
{
model: ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"]
CheckBox
{
text: modelData
checked: (days & (1<<index)) != false
onClicked:
{
if (checked) days |= (1<<index);
else days &= ~(1<<index);
}
}
}
Button
{
text: "clear all"
onClicked: days = 0
}
Button
{
text: "set all"
onClicked: days = 127
}
}
}
which looks like this:
To reproduce the problem, first click on "set all" and "clear all". then click on some checkboxes. Then click on "set all" and "clear all" again. You'll see that the boxes you checked are no longer affected.
thanks.
When you manually click the checkbox, the checked
property gets reassigned to a hardcoded true
instead of the original expression: (days & (1<<index)) != false
. Likewise, a manual uncheck of the box forces the checked
property to a hardcoded false
.
The fix is to simply rebind the checked
property using Qt.binding. I've cleaned up your javascript and fixed your bug. You are welcome.
Repeater
{
model: ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"]
CheckBox
{
function isChecked() {
return ((days & (1 << index)) != 0);
}
text: modelData
checked: isChecked()
onClicked:
{
if (checked) {
days |= (1<<index);
}
else {
days &= ~(1<<index);
}
// now rebind the item's checked property
checked = Qt.binding(isChecked);
}
}
}