C++與QML進行數(shù)據(jù)交互的常見方法總結(jié)
一、屬性綁定
這是最簡單的方式,可以在QML中直接綁定C++ 對象的屬性。通過在C++ 對象中使用Q_PROPERTY宏定義屬性,然后在QML中使用綁定語法將屬性與QML元素關(guān)聯(lián)起來。
1.person.h
#include <QObject> class Person : public QObject { Q_OBJECT /* 使用 Q_PROPERTY 定義交互的屬性 */ Q_PROPERTY(QString name READ getName WRITE setName NOTIFY nameChanged) Q_PROPERTY(int age READ getAge WRITE setAge NOTIFY ageChanged) public: explicit Person(QObject *parent = nullptr) : QObject(parent), m_name(""), m_age(0) { } /* 為屬性提供 getter 和 setter 方法 */ QString getName() const { return m_name; } void setName(const QString& name) { m_name = name; emit nameChanged(); } int getAge() const { return m_age; } void setAge(int age) { m_age = age; emit ageChanged(); } signals: /* 信號與屬性對應,通過信號通知其他對象屬性的變化 */ void nameChanged(); void ageChanged(); private: QString m_name; int m_age; };
2.main.cpp
#include <QGuiApplication> #include <QQmlApplicationEngine> #include <QQmlContext> #include "person.h" int main(int argc, char *argv[]) { /* 啟用Qt應用程序的高DPI縮放功能 */ QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling); /* 創(chuàng)建一個Qt應用程序的實例 */ QGuiApplication app(argc, argv); // 創(chuàng)建Person對象 Person person; QQmlApplicationEngine engine; /* 將Person對象作為QML上下文屬性 */ engine.rootContext()->setContextProperty("person", &person); const QUrl url(QStringLiteral("qrc:/main.qml")); /* 將 QQmlApplicationEngine 對象的 objectCreated 信號連接到一個 lambda 函數(shù)上 */ /* lambda 函數(shù)用于在 QML 文件中的根對象被創(chuàng)建時進行處理,檢查對象是否成功創(chuàng)建,如果創(chuàng)建失敗則退出應用程序 */ QObject::connect(&engine, &QQmlApplicationEngine::objectCreated, &app, [url](QObject *obj, const QUrl &objUrl) { if (!obj && url == objUrl) QCoreApplication::exit(-1); }, Qt::QueuedConnection); /* 加載QML文件并顯示用戶界面 */ engine.load(url); return app.exec(); }
3.main.qml
import QtQuick 2.12 import QtQuick.Window 2.12 import QtQuick.Controls 2.5 Window { visible: true width: 480 height: 800 title: qsTr("Hello World") Column { spacing: 10 TextField { placeholderText: "請輸入姓名" text: person.name // 與Person對象的name屬性綁定 onTextChanged: person.name = text // 當文本改變時,更新Person對象的name屬性 } Slider { from: 0 to: 100 value: person.age // 與Person對象的age屬性綁定 onValueChanged: person.age = value // 當滑塊值改變時,更新Person對象的age屬性 } Text { text: "姓名:" + person.name } Text { text: "年齡:" + person.age } } }
二、信號與槽
C++ 對象可以發(fā)出信號,而QML中的元素可以連接到這些信號上。這樣,當C++ 對象的狀態(tài)發(fā)生變化時,可以通過信號與槽機制將這些變化傳遞給QML界面。
1.myobject.h
#include <QObject> #include <QtDebug> class MyObject : public QObject { Q_OBJECT public: explicit MyObject(QObject *parent = nullptr) : QObject(parent) {} signals: void mySignal(QString message); public slots: void mySlot(const QString& message) { qDebug() << "Received message from QML:" << message; emit mySignal("Hello from C++");} };
2.main.cpp
#include <QGuiApplication> #include <QQmlApplicationEngine> #include <QQmlContext> #include "myobject.h" int main(int argc, char *argv[]) { /* 啟用Qt應用程序的高DPI縮放功能 */ QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling); /* 創(chuàng)建一個Qt應用程序的實例 */ QGuiApplication app(argc, argv); /* 將自定義 C++ 類型注冊到 QML 中的函數(shù), 將自定義 C++ 類型注冊到 QML 中的函數(shù) */ qmlRegisterType<MyObject>("com.example", 1, 0, "MyObject"); QQmlApplicationEngine engine; const QUrl url(QStringLiteral("qrc:/main.qml")); /* 將 QQmlApplicationEngine 對象的 objectCreated 信號連接到一個 lambda 函數(shù)上 */ /* lambda 函數(shù)用于在 QML 文件中的根對象被創(chuàng)建時進行處理,檢查對象是否成功創(chuàng)建,如果創(chuàng)建失敗則退出應用程序 */ QObject::connect(&engine, &QQmlApplicationEngine::objectCreated, &app, [url](QObject *obj, const QUrl &objUrl) { if (!obj && url == objUrl) QCoreApplication::exit(-1); }, Qt::QueuedConnection); /* 加載QML文件并顯示用戶界面 */ engine.load(url); return app.exec(); }
3.main.qml
import QtQuick 2.12 import QtQuick.Window 2.12 import QtQuick.Controls 2.5 import com.example 1.0 Window { visible: true width: 480 height: 800 title: qsTr("Hello World") /* 定義 sendToCpp 信號 */ signal sendToCpp(string message) /* Connections 組件用于連接 myObject 的 onMySignal 信號 */ Connections { target: myObject onMySignal: console.log("Received message from C++:", message) } MyObject { id: myObject /* 將 onMySignal 信號傳遞到 sendToCpp信號上,便于 QML 處理 */ onMySignal: sendToCpp(message) } Button { text: "Send message to C++" anchors.centerIn: parent /* 單擊按鈕時,會將信號傳遞到 C++ 的 mySlot 槽上 */ onClicked: myObject.mySlot("Hello from QML") } }
三、模型視圖
模型視圖(Model-View):可以使用C++ 中的數(shù)據(jù)模型(QStandardItemModel)來提供數(shù)據(jù)給QML界面。QML中的視圖元素(如ListView或GridView)可以使用這些模型來顯示數(shù)據(jù)。
1.mymodel.h
#ifndef MYMODEL_H #define MYMODEL_H #include <QAbstractListModel> #include <QList> class MyModel : public QAbstractListModel { Q_OBJECT public: explicit MyModel(QObject *parent = nullptr); enum { NameRole = Qt::UserRole + 1, AgeRole, EmailRole }; // 重寫以下幾個虛函數(shù) int rowCount(const QModelIndex &parent = QModelIndex()) const override; QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override; QHash<int, QByteArray> roleNames() const override; private: struct Person { QString name; int age; QString email; }; QList<Person> m_persons; }; #endif // MYMODEL_H
2.mymodel.cpp
#include "mymodel.h" MyModel::MyModel(QObject *parent) : QAbstractListModel(parent) { // 初始化一些數(shù)據(jù) m_persons.append({"Alice", 25, "alice@example.com"}); m_persons.append({"Bob", 30, "bob@example.com"}); m_persons.append({"Charlie", 35, "charlie@example.com"}); } int MyModel::rowCount(const QModelIndex &parent) const { Q_UNUSED(parent); return m_persons.count(); } QVariant MyModel::data(const QModelIndex &index, int role) const { if (!index.isValid()) return QVariant(); if (index.row() >= m_persons.count() || index.row() < 0) return QVariant(); const Person &person = m_persons[index.row()]; if (role == NameRole) return person.name; else if (role == AgeRole) return person.age; else if (role == EmailRole) return person.email; return QVariant(); } QHash<int, QByteArray> MyModel::roleNames() const { QHash<int, QByteArray> roles; roles[NameRole] = "name"; roles[AgeRole] = "age"; roles[EmailRole] = "email"; return roles; }
3.main.cpp
#include <QGuiApplication> #include <QQmlApplicationEngine> #include <QQmlContext> #include "mymodel.h" int main(int argc, char *argv[]) { /* 啟用Qt應用程序的高DPI縮放功能 */ QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling); /* 創(chuàng)建一個Qt應用程序的實例 */ QGuiApplication app(argc, argv); QQmlApplicationEngine engine; MyModel myModel; engine.rootContext()->setContextProperty("myModel", &myModel); const QUrl url(QStringLiteral("qrc:/main.qml")); /* 將 QQmlApplicationEngine 對象的 objectCreated 信號連接到一個 lambda 函數(shù)上 */ /* lambda 函數(shù)用于在 QML 文件中的根對象被創(chuàng)建時進行處理,檢查對象是否成功創(chuàng)建,如果創(chuàng)建失敗則退出應用程序 */ QObject::connect(&engine, &QQmlApplicationEngine::objectCreated, &app, [url](QObject *obj, const QUrl &objUrl) { if (!obj && url == objUrl) QCoreApplication::exit(-1); }, Qt::QueuedConnection); /* 加載QML文件并顯示用戶界面 */ engine.load(url); return app.exec(); }
4.main.qml
import QtQuick 2.12 import QtQuick.Window 2.12 import QtQuick.Controls 2.5 Window { visible: true width: 480 height: 800 title: qsTr("Hello World") ListView { anchors.fill: parent model: myModel delegate: Item { width: parent.width height: 60 Column { Text { text: name } Text { text: age } Text { text: email } } } } }
運行效果
四、QML類型注冊
QML類型注冊(QML Type Registration):可以將C++ 對象注冊為自定義的QML類型,使得QML可以直接創(chuàng)建和使用這些對象。通過在C++ 中使用 Q_PROPERTY 宏和 Q_INVOKABLE 函數(shù),可以將C++ 類注冊為QML類型。我需要這樣一個案例
1.myobject.h
#include <QQmlEngine> #include "QDebug" class MyObject : public QObject { Q_OBJECT Q_PROPERTY(QString name READ name WRITE setName NOTIFY nameChanged) public: explicit MyObject(QObject *parent = nullptr) : QObject(parent) {} QString name() const { return m_name; } void setName(const QString &name) { m_name = name; emit nameChanged(); } Q_INVOKABLE void printName() { qDebug() << "Name:" << m_name; } static void registerQmlType() { qmlRegisterType<MyObject>("com.example", 1, 0, "MyObject"); } signals: void nameChanged(); private: QString m_name; };
2.main.cpp
#include <QGuiApplication> #include <QQmlApplicationEngine> #include <QQmlContext> #include "myobject.h" int main(int argc, char *argv[]) { /* 啟用Qt應用程序的高DPI縮放功能 */ QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling); /* 創(chuàng)建一個Qt應用程序的實例 */ QGuiApplication app(argc, argv); QQmlApplicationEngine engine; MyObject::registerQmlType(); const QUrl url(QStringLiteral("qrc:/main.qml")); /* 將 QQmlApplicationEngine 對象的 objectCreated 信號連接到一個 lambda 函數(shù)上 */ /* lambda 函數(shù)用于在 QML 文件中的根對象被創(chuàng)建時進行處理,檢查對象是否成功創(chuàng)建,如果創(chuàng)建失敗則退出應用程序 */ QObject::connect(&engine, &QQmlApplicationEngine::objectCreated, &app, [url](QObject *obj, const QUrl &objUrl) { if (!obj && url == objUrl) QCoreApplication::exit(-1); }, Qt::QueuedConnection); /* 加載QML文件并顯示用戶界面 */ engine.load(url); return app.exec(); }
3.main.qml
import QtQuick 2.12 import QtQuick.Window 2.12 import QtQuick.Controls 2.5 import com.example 1.0 Window { visible: true width: 480 height: 800 title: qsTr("Hello World") MyObject { id: myObject name: "John" } /* 垂直布置組件 */ Column { anchors.fill: parent // 大小為父組件的大小 anchors.margins: 40 // 與父組件四周的間隔 spacing: 10 // 子組件之間的間隔 Text { text: myObject.name } Button { text: "Print Name" onClicked: myObject.printName() } } }
到此這篇關(guān)于C++與QML進行數(shù)據(jù)交互的常見方法總結(jié)的文章就介紹到這了,更多相關(guān)C++ QML數(shù)據(jù)交互內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C語言實現(xiàn)航空訂票系統(tǒng)課程設(shè)計
這篇文章主要為大家詳細介紹了C語言實現(xiàn)航空訂票系統(tǒng)課程設(shè)計,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-03-03C++實現(xiàn)約瑟夫環(huán)的循環(huán)單鏈表
這篇文章主要為大家詳細介紹了C++實現(xiàn)約瑟夫環(huán)的循環(huán)單鏈表,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-10-10C/C++自主分配出現(xiàn)double free or corruption問題解決
這篇文章主要為大家介紹了C/C++出現(xiàn)double free or corruption問題解決,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-04-04