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,
You can also make two-way binding to resolve this issue:
Sometimes it is useful to separate input and output values in control. In this case control always displays real value and it can also show a delay to the user.
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, thechecked: 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: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. CatchonSomeSettingChanged
signal and explicitly changed the check box. Submit the value ofsomeSettingCheckBox
toSettings
only when the program/widget/dialog/xxx finished.If you don't want to make a binding loop - don't make a binding, use a proxy variable, for example. Other simple solution can be to check the value: