QML&C++应用程序中的绑定循环问题



我正在进行一个项目,基本上我想做的是在2个QML文件中的2个盒子之间同步属性 theProperty(两个都有该属性(,我将 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在另一侧,是否有一种方法可以在此问题上行走?

为什么不这样做:

Box_A {
    id: box_A
    // sth
    theProperty:model.CppModel.theProperty  
}

那对您不起作用吗?

最新更新