qt - 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 {
}
}
}
解决方案
你的代码没问题。这只是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 {
}
}
}
推荐阅读
- python - cp_model.CpModel() 中是否有与 pywrapcp.Solver() 中的 IsEqualCstVar 类似的功能?
- flutter - 如何按组计算列表中的金额总和?
- python - 在 google colab 中与其他协作者共享文件
- c++ - 我应该如何使用epoll从同一个FD读取和写入
- reactjs - React:使用索引作为列表中项目的键
- php - 我需要一个域来设置 Apache 服务器吗?
- javascript - 如何在 xslt 脚本中导入 Javascript 对象
- python - Python-AttributeError: 'NoneType' 对象没有属性 'find';;;
- java - Spring Cloud AWS SQS 无法在本地连接到服务端点
- angular - HttpParams 在 Angular 中不起作用,出现 502 错误