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

I study QtQuick and I play 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 username using setNamefrom my C ++ code, it is automatically displayed in the view. When I check / uncheck the box, or when I call setStatus()from my C ++ code, nothing happens. It seems that the checkedflags property does not have the same behavior as TextEdit.

I do not want to bind my properties in a declarative way. Is the Qt Quick binding property bound?

Thank you for your help.

+4
2

Leemes, , , . , , , "". "onClicked" "". Component.onCompleted(). ...

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

( , ) onClicked, :

CheckBox {
    checked: user.status
    onClicked: {
        user.status = checked;
        checked = Qt.binding(function () { // restore the binding
            return user.status;
        });
    }
}

, Component.onCompleted.

+2

Source: https://habr.com/ru/post/1541975/


All Articles