Binding Checkbox 'checked' property with a C++ object Q_PROPERTY

c++, checkbox, data-binding, qt, qt-quick

Solution

As leemes points out, user clicking the check box breaks the binding you've created. So, don't create the binding, but instead connect to the change signal directly to handle the "get" case. Use "onClicked" to handle the "set" case. This solution requires you also initialize in Component.onCompleted(). For example...

CheckBox {
    id: myCheck
    onClicked: user.status = checked
    Component.onCompleted: checked = user.status
    Connections {
        target: user
        onStatusChanged: myCheck.checked = user.status
    }
}

Problem

I'm learning QtQuick and I'm playing with data binding between C++ classes and QML properties. In my C++ object Model, I have two properties : ``` Q_PROPERTY(QString name READ getName WRITE setName NOTIFY nameChanged) Q_PROPERTY(bool status READ getStatus WRITE setStatus NOTIFY statusChanged) ``` And in my .qml file : ``` TextEdit { placeholderText: "Enter your name" text: user.name } Checkbox { checked: user.status } ``` When I change the user name with `setName` from my C++ code, it is automatically reflected in the view. When I check/uncheck the checkbox, or when I call `setStatus()` from my C++ code, nothing happens. It seems the property `checked` of checkboxes haven't the same behavior as `TextEdit` components. I don't want to bind my properties in a declarative way. Doesn't Qt Quick support property binding ? Thank you for your help.

Original source