3
votes

I trying to understand how bindings works when dynamic objects are used. And don't understand anything.

In code bellow I have "static" bindings:

property bool flag1: cfg_flag1

and create dynamic binding that set flag1 to true, then I destroy binding and make sure that it really destroyed (via logs), after that I trigger initial binding, but looks like binding restoring doesn't work, it prints:

qmlscene /tmp/Test.qml
qml: set flag1 to true
qml: buggon1 cliecked
qml: end of clicked
qml: destroyed
qml: timer trigger

So restoreMode: Binding.RestoreBinding doesn't restore previous binding or I missed something?

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 {
        }

    }
}
1
Please provide your Binding object code. As for the question, I guess that Binding.RestoreBinding (that is default value) restores binding on the same instance. In your case you delete it and create another instance.folibis
@folibis This is full code, you can run it via qmlscene. Not sure what do you mean by providing binding code.user1244932
ah, I see you create its properties from the createObject call, missed that.folibis

1 Answers

1
votes

Your code is OK. This is just another bug of Binding =/.

To make it work, you can add

  • "when": true to list of properties for temp
  • temp.when = false; on temp's destruction

Here is your edited code

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,
                "when": true
            });
            temp.Component.onDestruction.connect(function() { temp.when = false; console.log("destroyed"); });
            temp.destroy();
            console.log("end of clicked");
            timer.start();
        }
    }

    Component {
        id: cmpBinding

        Binding {
        }

    }
}