1

I am doing a project, basically what I want to do is to synchronize a property theProperty between 2 boxes in 2 QML files(2 boxes both own that property), I bind theProperty to a C++ Q_PROPERTY, so by binding the 2 boxes to the same C++ Q_PROPERTY, the synchronization can be achieved. Here are my codes in Box A and B. theProperty can be independently changed by Box A and 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
    }
}

In cpp:

    Class Item: QObject{
        Q_OBJECT
        Q_PROPERTY(bool theProperty READ theProperty WRITE theProperty NOTIFY theProperty Changed)
  //sth
        }

Within Box_A and B, there is a mouse area by which the theProperty can be changed:

MouseArea{

  onClicked: theProperty=!theProperty
}

The problem is that once I change theProperty in either box A or B, the qt creator complains that loop binding detected for value: model.CppModel.theProperty at the other side, is there a way of walking around this problem?

WSL
  • 13
  • 6

1 Answers1

0

Why don't you just do:

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

Did that not work for you?

bardao
  • 914
  • 12
  • 23
  • I tried with another similar property, the weird thing was it worked at first but failed later, e.g. If I started with box_A and kept changing the property in Box_A, the property in Box_B could change accordingly, however if then I started to change that in Box_B, the "sync" between 2 boxes lost. What confused me more was that I used the above approach exactly the same for "another similar property", it worked well for me, but theProperty which I showed above didn't work with the approach – WSL May 01 '19 at 18:48
  • @WSL a quick hack for this is to emit a signal from `BOX_A` and `BOX_B` when that property changes, different than `onThePropertyChanged`, for example `signal propChanged(var prop)` and do `onPropChanged: model.CppModel.theProperty = prop` this will avoid a binding loop since it's not the property's default onChanged signal that is being called – bardao May 01 '19 at 20:03