【问题标题】:Qml how restoring of bindings works?Qml如何恢复绑定?
【发布时间】:2020-10-13 13:56:26
【问题描述】:

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

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

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

    }
}

【问题讨论】:

  • 请提供您的绑定对象代码。至于这个问题,我猜Binding.RestoreBinding(即默认值)会恢复同一个实例上的绑定。在您的情况下,您将其删除并创建另一个实例。
  • @folibis 这是完整代码,您可以通过qmlscene 运行它。不知道提供绑定代码是什么意思。
  • 啊,我看到你通过createObject 调用创建了它的属性,错过了。

标签: qt qml qt5


【解决方案1】:

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

为了让它工作,你可以添加

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

这是您编辑的代码

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

    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-16
    • 1970-01-01
    相关资源
    最近更新 更多