C++中使用qml對象,直接使用findChild獲取qml對象,而後調用setProperty方法設置屬性,固然必須在加載qml以後才能使用,否則findChild找不到對象,用法以下。測試
engine.load(QUrl(QStringLiteral("qrc:/main.qml"))); QObject * text_Msg = engine.rootObjects()[0]->findChild<QObject*>("text_Msg"); text_Msg->setProperty("color","red");
qml使用C++對象,這也是Qt中Model/View的實現方法,下面是一個例子。ui
首先,類須要繼承自QObejctthis
class User:public QObject { Q_OBJECT Q_PROPERTY(QString Name READ Name WRITE setName NOTIFY NameChanged) Q_PROPERTY(int Age READ Age WRITE setAge NOTIFY AgeChanged) Q_PROPERTY(QString Message READ Message WRITE setMessage NOTIFY MessageChanged) public: User(); User(string name,int age); QString Name(); void setName(QString name); int Age(); void setAge(int age); QString Message(); void setMessage(QString message); signals: void NameChanged(); void AgeChanged(); void MessageChanged(); public slots: void editOk(); private : QString m_name; int m_age; QString m_message; };
User::User() { this->setName(""); this->setAge(0); } User::User(string name, int age) { QString q_name = QString::fromStdString(name); this->setName(q_name); this->setAge(age); } QString User::Name() { return m_name; } void User::setName(QString name) { m_name = name; emit NameChanged(); setMessage(QString("名稱改變爲:%1").arg(name)); } int User::Age() { return m_age; } void User::setAge(int age) { m_age = age; emit AgeChanged(); setMessage(QString("年齡改變爲:%1").arg(age)); } QString User::Message() { return m_message; } void User::setMessage(QString message) { m_message = message; emit MessageChanged(); } void User::editOk() { setMessage("您點擊了肯定按鈕"); }
而後,須要向qml中註冊這個類,這樣咱們在qml中就能夠導入這個類了spa
qmlRegisterType<User>("Models.User",1,0,"UserModel");
import Models.User 1.0
一般,直接將對象設置到qml對象的上下文,而後在qml中使用C++對象的屬性code
QQmlApplicationEngine engine; QQmlContext* context = engine.rootContext(); User* userModel = new User("測試",20); context->setContextProperty("testUserModel",userModel);
GridLayout{ anchors.centerIn: parent width:300 columnSpacing: 10 rowSpacing: 10 rows:4 columns:2 Item{ Layout.row: 0 Layout.column: 0 width:100 height:30 Text{ text:"Name:" anchors.centerIn: parent } } Item{ Layout.row: 0 Layout.column: 1 Layout.fillWidth: true height:30 TextField{ anchors.verticalCenter: parent.verticalCenter width:parent.width height:24 id:textfield_Name text: testUserModel.Name onEditingFinished: { testUserModel.Name = textfield_Name.text; } } } Item{ Layout.row: 1 Layout.column: 0 width:100 height:30 Text{ text:"Age:" anchors.centerIn: parent } } Item{ Layout.row: 1 Layout.column: 1 Layout.fillWidth: true height:30 TextField{ anchors.verticalCenter: parent.verticalCenter width:parent.width height:24 id:textfield_Age text:testUserModel.Age onEditingFinished: { testUserModel.Age = textfield_Age.text; } } } Item{ Layout.row: 2 Layout.column: 0 Layout.fillWidth: true height: 50 Layout.columnSpan: 2 RowLayout{ anchors.centerIn: parent Button{ id:button_OK text:"肯定" action: button_OK_Action onClicked:{ testUserModel.editOk(); } } Button{ id:button_Cancel text:"取消" onClicked: { Qt.quit(); } } } } Item{ Layout.row: 3 Layout.column: 0 Layout.fillWidth: true height: 30 Layout.columnSpan: 2 Text { id: text_Msg objectName: "text_Msg" text: testUserModel.Message anchors.fill: parent verticalAlignment: Qt.AlignVCenter } } }
在qml中綁定C++對象的屬性時,實際上是執行C++對象屬性的READ方法;而設置屬性時,則是執行WRITE方法;C++屬性的NOTIFY方法用於屬性變動通知,當咱們調用該方法時(一般在前面加上emit表示這是個信號方法),qml中的屬性綁定就會再次執行READ方法。對象
下面是效果,編輯框失去焦點時,就會設置屬性,進而調用C++對象的WRITE方法,而後在WRITE方法中更新其餘屬性。blog