4
votes

I'm facing a big problem that it's taking a lot of time to be fixed because I don't know the cause and how to fix it. The problem is really simple: I have an example QML component defined as:

Rectangle {
    id: rect
    property bool test: myclass.testproperty

    Rectangle {
        width: 50
        height: 50
        visible: parent.test
    }
}

and I connected a MouseArea onClicked signal to do this:

test = !test

so I switch the value of the boolean variable. To push the value from C++ to QML and from QML to C++ Q_PROPERTY with READ, WRITE and NOTIFY signals, I used this

Binding {
    target: myclass
    property: "testproperty"
    value: rect.test
}

everything works fine until I click on the mouseArea and so I push the changes via the binding. After that, every time I try to set a new property value from C++ I don't see any change in QML, like if the binding is destroyed. But if I try to click on the MouseArea I still call the setTestProperty method of the C++ class. Basically, it goes out of sync the C++ -> QML way. Why? I can't find the problem, the signal is emitted because QSignalSpy gives me 1 as count of emitted times after using

emit this->testPropertyChanged(newvalue)

EDIT

here an example: basically here we're using a QString property with the same exact signals. The only thing that changes is that instead of using a Rectangle QML element and binding to a own property, I'm using a TextInput element

TextInput {
    id: mytext
    text: myclass.testproperty
}

Binding {
    target: myclass
    property: "testproperty"
    value: mytext.text
}
1

1 Answers

7
votes

There is no problem here. It is a standard QML bindings behaviour. When you change some QML Widget's property in JavaScript code, then all declarative bindings to it will be terminated. It is your choice to use declarative binding or update values manually in JS code of event handlers.

EDIT

Rectangle {
    id: rect

    // Establishing initial value for 'test' property via QML binding
    property bool test: myclass.testproperty

    // Manual update of 'test' property when binding will be broken
    Connections {
        target: myclass
        onTestpropertyChanged: {
            rect.test = xxx
        }
    }
}