Qt 5.7 QML Why do my CheckBox property bindings disappear?

I have a simple CheckBox es list, one for each day of the week. They depend on the value of days , an integer using a mask, 1 bit for each CheckBox .

The purpose of days is with both the “clear all” button and the “install all” button, and they are updated. However, after clicking on any of the mailboxes, they will no longer respond to changes in the days dependent property.

Why is this? They somehow become disconnected. If so, do I have to manually bandage them, and if so, why?

Here is 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 is as follows:

enter image description here

To reproduce the problem, first click Install All and Clear All. then click on some checkboxes. Then click Install All and Clear All again. You will see that the checkboxes you checked are no longer affected.

thanks.

+5
source share
2 answers

When you manually check the box, the checked property will be reassigned to hardcoded true instead of the original expression: (days & (1<<index)) != false . Similarly, manually unchecking the check causes the checked property to be hardcoded false .

The fix is ​​to simply restore the checked property using Qt.binding . I cleared your javascript and fixed the error. 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 checked property checked = Qt.binding(isChecked); } } } 
+1
source

OP is here.

Selby's answer is quite correct. But I would like to publish the option that I prefer.

I came to the conclusion that CheckBox es are broken in QT. This is because you want to bind them to your data model. , and you also want to click them (otherwise, that point). Clicking on them will break the connection with the model, so you need to configure it manually (see Selbie's answer). For me it's a broken design.

My version uses Binding , so it doesn’t need to be restored every time you click.

like this:

 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 Binding on checked { value: (days & (1 << index)) != 0 } onClicked: { if (checked) days |= (1<<index) else days &= ~(1<<index) } } } Button { text: "clear all" onClicked: days = 0 } Button { text: "set all" onClicked: days = 127 } } } 

Publish this option to others.

+2
source

All Articles