【发布时间】:2019-09-20 03:22:18
【问题描述】:
我正在做一个项目,基本上我想做的是在 2 个 QML 文件中的 2 个盒子之间同步一个属性 theProperty(2 个盒子都拥有该属性),我将 theProperty 绑定到 C++ Q_PROPERTY,所以通过将 2 个框绑定到相同的 C++ Q_PROPERTY,可以实现同步。
这是我在 Box A 和 B 中的代码。theProperty 可以由 Box A 和 B 独立更改
Box_A {
id: box_A
// sth
Binding { target:box_A; property: "theProperty"; value:model.CppModel.theProperty }
onThePropertyChanged: {
model.CppModel.theProperty = theProperty
}
}
Box_B {
id: box_B
// sth
Binding { target:box_B; property: "theProperty"; value:model.CppModel.theProperty }
onThePropertyChanged: {
model.CppModel.theProperty = theProperty
}
}
在 cpp 中:
Class Item: QObject{
Q_OBJECT
Q_PROPERTY(bool theProperty READ theProperty WRITE theProperty NOTIFY theProperty Changed)
//sth
}
在 Box_A 和 B 内,有一个鼠标区域可以更改 theProperty:
MouseArea{
onClicked: theProperty=!theProperty
}
问题是,一旦我在框 A 或 B 中更改 theProperty,qt 创建者抱怨循环绑定检测到值:model.CppModel.theProperty 在另一边,有没有办法解决这个问题?
【问题讨论】:
-
使用这个解决方案:stackoverflow.com/a/40764480/6622587