Qt 5.4/Qml: Prevent binding loop

前端 未结 5 1318
孤街浪徒
孤街浪徒 2021-02-19 04:04

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 \"ch

5条回答
  •  生来不讨喜
    2021-02-19 04:40

    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
    

提交回复
热议问题