如何将 C++ 属性绑定到 QML 属性?

2022-01-19 00:00:00 qt qml c++ qobject

所以我知道如何将 QML 属性绑定到 C++ 属性,所以当 C++ 调用通知信号时,QML 会更新视图.当用户使用 UI 更改某些内容时,有什么方法可以更新 C++ 属性?

So I know how to bind QML property to C++ property, so when C++ one calls notify signal, QML updates the view. Is there any way to make C++ property update when user changes something using UI?

例如,我有一个组合框,我希望在用户更改组合框的值时更新一些 C++ 属性.

For example, I have a Combobox, and I want some C++ property to be updated when user changes value of combobox.

我所说的 C++ 属性是指 QObject 派生类中的 Q_PROPERTY 宏.

By C++ properties I mean Q_PROPERTY macro in QObject-derived classes.

推荐答案

要绑定不是在 QML 中创建(或在其他上下文中创建)的对象的属性,您必须使用 绑定.在你的情况下:

To bind a property from an object you didn't create in QML (or was created in another context), you have to use Binding. In your case :

Binding {
    target: yourCppObject
    property: "cppPropertyName"
    value: yourComboBox.currentText
}

相关文章