Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Two bindings for the same property?



  • I want two binding for the same field, one default,
    and other should replace default and then restore it back.

    As I understand Qt/QML can do it: https://doc.qt.io/qt-5/qml-qtqml-binding.html via restoreMode : Binding.RestoreBinding.

    But for some reason it doesn't work.
    I create static binding: cfg_flag1 -> flag1, then I create temporary bindingtrue -> flag1and destroy this binding, then I setcfg_flag1 = falseand I expect that via restored bindingcfg_flag1 -> flag1flag1gotfalse`. But for some reason the static binding do not work anymore.
    Any idea about how RestoreBinding works?

    import QtQuick 2.0
    import QtQuick.Controls 2.15
    
    Rectangle {
        id: rect
        width: 100
        height: 100
        color: "red"
    
        property bool flag1: {
            console.log("set flag1 to", cfg_flag1);
            return cfg_flag1;
        }
        property bool cfg_flag1: true
    
        Text {
            anchors.centerIn: parent
            text: "flag1: " + flag1.toString() + ", cfg_flag1 " + cfg_flag1.toString()
        }
    
        Timer {
            id: timer
            interval: 1000
            repeat: false
            onTriggered: {
                console.log("timer trigger");
                cfg_flag1 = false;
            }
        }
    
        Button {
            anchors.top: parent.top
            text: "button 1"
            onClicked: {
                console.log("buggon1 cliecked");
                let temp = cmpBinding.createObject(rect, {
                    "target": rect,
                    "property": "flag1",
                    "value": true,
                    "restoreMode": Binding.RestoreBinding,
                });
                temp.Component.onDestruction.connect(function() { console.log("destroyed"); });
                temp.destroy();
                console.log("end of clicked");
                timer.start();
            }
        }
    
        Component {
            id: cmpBinding
    
            Binding {
            }
    
        }
    }
    

Log in to reply