如何将 C++ 属性 绑定到 QML 属性?
How to bind C++ property to QML property?
所以我知道如何将 QML 属性 绑定到 C++ 属性,所以当 C++ 调用通知信号时,QML 会更新视图。当用户使用 UI 更改某些内容时,是否有任何方法可以使 C++ 属性 更新?
例如,我有一个组合框,我希望在用户更改组合框的值时更新一些 C++ 属性。
编辑:通过 C++ 属性,我的意思是 Q_PROPERTY
宏在 QObject
派生的 类.
1) Firstly you have to create main.cpp page.
#include <QtGui>
#include <QtDeclarative>
class Object : public QObject
{
Q_OBJECT
Q_PROPERTY( QString theChange READ getTheChange NOTIFY changeOfStatus )
public:
Object() {
changeMe = false;
myTimer = new QTimer(this);
myTimer->start(5000);
connect(myTimer, SIGNAL (timeout()), this, SLOT (testSlot()));
}
QString getTheChange() {
if (theValue 0) {
return "The text changed";
} if (theValue 1) {
return "New text change";
}
return "nothing has happened yet";
}
Q_INVOKABLE void someFunction(int i) {
if ( i 0) {
theValue = 0;
}
if (i 1) {
theValue = 1;
}
emit changeOfStatus(i);
}
signals:
void changeOfStatus(int i) ;
public slots:
void testSlot() {
if (changeMe) {
someFunction(0);
} else {
someFunction(1);
}
changeMe = !changeMe;
}
private:
bool changeMe;
int theValue;
QTimer *myTimer;
};
#include "main.moc"
int main(int argc, char* argv[])
{
QApplication app(argc, argv);
Object myObj;
QDeclarativeView view;
view.rootContext()->setContextProperty("rootItem", (QObject *)&myObj);
view.setSource(QUrl::fromLocalFile("main.qml"));
view.show();
return app.exec();
}
2) The QML Implementation main.qml
In the QML code below we create a Rectangle that reacts to mouse clicks. The text is set to the result of the Object::theChange() function.
import QtQuick 1.0
Rectangle {
width: 440; height: 150
Column {
anchors.fill: parent; spacing: 20
Text {
text: rootItem.theChange
font.pointSize: 25; anchors.horizontalCenter: parent.horizontalCenter
}
}
}
So, using the approach in the example above, we get away for QML properties to react to changes that happen internally in the C++ code.
来源:https://wiki.qt.io/How_to_Bind_a_QML_Property_to_a_C%2B%2B_Function
要从您未在 QML 中创建(或在其他上下文中创建)的对象绑定 属性,您必须使用 Binding
。
在你的情况下:
Binding {
target: yourCppObject
property: "cppPropertyName"
value: yourComboBox.currentText
}
也许 answer to a similar question 会有所帮助。
它展示了如何将标准(非自定义)qml 属性 与 C++ 中的某些内容连接起来。
所以我知道如何将 QML 属性 绑定到 C++ 属性,所以当 C++ 调用通知信号时,QML 会更新视图。当用户使用 UI 更改某些内容时,是否有任何方法可以使 C++ 属性 更新?
例如,我有一个组合框,我希望在用户更改组合框的值时更新一些 C++ 属性。
编辑:通过 C++ 属性,我的意思是 Q_PROPERTY
宏在 QObject
派生的 类.
1) Firstly you have to create main.cpp page.
#include <QtGui>
#include <QtDeclarative>
class Object : public QObject
{
Q_OBJECT
Q_PROPERTY( QString theChange READ getTheChange NOTIFY changeOfStatus )
public:
Object() {
changeMe = false;
myTimer = new QTimer(this);
myTimer->start(5000);
connect(myTimer, SIGNAL (timeout()), this, SLOT (testSlot()));
}
QString getTheChange() {
if (theValue 0) {
return "The text changed";
} if (theValue 1) {
return "New text change";
}
return "nothing has happened yet";
}
Q_INVOKABLE void someFunction(int i) {
if ( i 0) {
theValue = 0;
}
if (i 1) {
theValue = 1;
}
emit changeOfStatus(i);
}
signals:
void changeOfStatus(int i) ;
public slots:
void testSlot() {
if (changeMe) {
someFunction(0);
} else {
someFunction(1);
}
changeMe = !changeMe;
}
private:
bool changeMe;
int theValue;
QTimer *myTimer;
};
#include "main.moc"
int main(int argc, char* argv[])
{
QApplication app(argc, argv);
Object myObj;
QDeclarativeView view;
view.rootContext()->setContextProperty("rootItem", (QObject *)&myObj);
view.setSource(QUrl::fromLocalFile("main.qml"));
view.show();
return app.exec();
}
2) The QML Implementation main.qml
In the QML code below we create a Rectangle that reacts to mouse clicks. The text is set to the result of the Object::theChange() function.
import QtQuick 1.0
Rectangle {
width: 440; height: 150
Column {
anchors.fill: parent; spacing: 20
Text {
text: rootItem.theChange
font.pointSize: 25; anchors.horizontalCenter: parent.horizontalCenter
}
}
}
So, using the approach in the example above, we get away for QML properties to react to changes that happen internally in the C++ code.
来源:https://wiki.qt.io/How_to_Bind_a_QML_Property_to_a_C%2B%2B_Function
要从您未在 QML 中创建(或在其他上下文中创建)的对象绑定 属性,您必须使用 Binding
。
在你的情况下:
Binding {
target: yourCppObject
property: "cppPropertyName"
value: yourComboBox.currentText
}
也许 answer to a similar question 会有所帮助。
它展示了如何将标准(非自定义)qml 属性 与 C++ 中的某些内容连接起来。