首页 > 解决方案 > Qml如何恢复绑定?

问题描述

我试图了解使用动态对象时绑定是如何工作的。而且什么都不懂。

在下面的代码中,我有“静态”绑定:

property bool flag1: cfg_flag1

并创建将 flag1 设置为 的动态绑定true,然后我销毁绑定并确保它确实被销毁(通过日志),之后我触发初始绑定,但看起来绑定恢复不起作用,它打印:

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

所以restoreMode: Binding.RestoreBinding不恢复以前的绑定还是我错过了什么?

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

    }
}

标签: qtqmlqt5

解决方案


你的代码没问题。这只是Binding=/ 的另一个错误。

要使其工作,您可以添加

  • "when": true到属性列表temp
  • temp.when = false;ontemp的破坏

这是您编辑的代码

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

    }
}

推荐阅读