I have a global singleton "Settings" which holds application settings. When I try to run the following code I get a QML CheckBox: Binding loop detected for property "checked"
:
CheckBox {
checked: Settings.someSetting
onCheckedChanged: {
Settings.someSetting = checked;
}
}
It is obvious why this error occurs, but how can I correctly implement this functionality without a binding loop? E.g. I want to save the current checked state of the checkbox in the settings singleton.
I am using Qt 5.4 and Qml Quick 2.
Regards,
Don't bind it. Because the check box does not fully depend on Setting.someSetting
.
When a user clicked the checkbox, the CheckBox.checked
is changed by itself. At the same time, the property binding is no longer valid. Settings.someSetting
cannot modify the CheckBox after it is clicked by user. Therefore, the checked: Settings.someSetting
binding is wrong.
If you want to assign an initial value to the check box when the component is ready, use Component.onCompleted
to assign it:
CheckBox {
id: someSettingCheckBox
Component.onCompleted: checked = Settings.someSetting
onCheckedChanged: Settings.someSetting = checked;
}
If you are working on a more complex scenario, the Setting.someSetting
may be changed by some other things during runtime and the state of the check box is required to be changed simultaneously. Catch onSomeSettingChanged
signal and explicitly changed the check box. Submit the value of someSettingCheckBox
to Settings
only when the program/widget/dialog/xxx finished.
CheckBox { id: someSettingCheckBox }
//within the Settings, or Connection, or somewhere that can get the signal.
onSomeSettingChanged: someSettingCheckBox.checked = someSetting