在Qt中,ComboBox(組合框)是一種常用的用戶界面控制項,它提供了一個下拉列表,允許用戶從預定義的選項中選擇一個。該組件提供了一種方便的方式讓用戶從預定義的選項中進行選擇,一般來說`ComboBox`會以按鈕的形式顯示在界面上,用戶點擊按鈕後,會彈出一個下拉列表,其中包含預定義的選項。當然`Co... ...
-
一、QML訪問C++方法
- Qt元對象系統中註冊C++類,在QML中實例化、訪問。
- C++中實例化並設置為QML上下文屬性,在QML中直接使用。
比較:方法1可以使C++類在QML中作為一個數據類型,例如函數參數類型或屬性類型,也可以使用其枚舉類型、單例等,功能更強大。
-
二、QML訪問C++條件
- 派生自QObject類或QObject類的子類
- 使用Q_OBJECT巨集
-
三、QML訪問C++舉例
使用方法1
信號與槽
#ifndef GEMINI_H #define GEMINI_H #include <QObject> #include <QDebug> class Gemini : public QObject { Q_OBJECT signals: //1、先定義“信號” void begin(); public slots: //1、先定義“槽” void doSomething() { qDebug() << "Gemini::doSomething() called"; } }; #endif
#include <QGuiApplication> #include <QQmlApplicationEngine> #include <QtQml> #include <Gemini.h> int main(int argc, char *argv[]) { QGuiApplication app(argc, argv); qmlRegisterType<Gemini>("Union.Lotto.Gemini", 1, 0, "Gemini"); //一.註冊c++類 QQmlApplicationEngine engine; engine.load(QUrl(QStringLiteral("qrc:///main.qml"))); return app.exec(); }
import QtQuick 2.2 import QtQuick.Window 2.1 import Union.Lotto.Gemini 1.0 //二.導入 Window { visible: true width: 360; height: 360 title: "Union Lotto Game" color: "white" MouseArea { anchors.fill: parent onClicked: { gemini.begin() //2.調用類信號 } } Gemini { id: gemini onBegin: doSomething() //2.調用類槽 } }
2、枚舉類型
#ifndef GEMINI_H #define GEMINI_H #include <QObject> #include <QDebug> class Gemini : public QObject { Q_OBJECT Q_ENUMS(BALL_COLOR) //1、先定義。枚舉類型在QML中使用,就用到了Q_ENUMS()巨集 public: Gemini() : m_ballColor(BALL_COLOR_YELLOW) { qDebug() << "Gemini::Gemini() called"; } enum BALL_COLOR { BALL_COLOR_YELLOW, BALL_COLOR_RED, BALL_COLOR_BLUE, BALL_COLOR_ALL }; signals: void begin(); public slots: void doSomething(BALL_COLOR ballColor) { qDebug() << "Gemini::doSomething() called with" << ballColor; if(ballColor != m_ballColor) { m_ballColor = ballColor; qDebug() << "ball color changed"; } } private: BALL_COLOR m_ballColor; }; #endif
import QtQuick 2.2 import QtQuick.Window 2.1 import Union.Lotto.Gemini 1.0 Window { visible: true width: 360; height: 360 title: "Union Lotto Game" color: "white" MouseArea { anchors.fill: parent onClicked: { gemini.begin() } } Gemini { id: gemini onBegin: doSomething(Gemini.BALL_COLOR_RED) //2.調用,在QML中使用枚舉類型的方式是<CLASS_NAME>.<ENUM_VALUE> } }
3、成員函數
#ifndef GEMINI_H #define GEMINI_H #include <QObject> #include <QDebug> class Gemini : public QObject { Q_OBJECT Q_ENUMS(BALL_COLOR) public: Gemini() : m_ballColor(BALL_COLOR_YELLOW) { qDebug() << "Gemini::Gemini() called"; } enum BALL_COLOR { BALL_COLOR_YELLOW, BALL_COLOR_RED, BALL_COLOR_BLUE, BALL_COLOR_ALL }; Q_INVOKABLE void stop() { //1、定義。訪問的前提是public或protected成員函數,且使用Q_INVOKABLE巨集 qDebug() << "Gemini::stop() called"; } signals: void begin(); public slots: void doSomething(BALL_COLOR ballColor) { qDebug() << "Gemini::doSomething() called with" << ballColor; if(ballColor != m_ballColor) { m_ballColor = ballColor; qDebug() << "ball color changed"; } } private: BALL_COLOR m_ballColor; }; #endif
import QtQuick 2.2 import QtQuick.Window 2.1 import Union.Lotto.Gemini 1.0 Window { visible: true width: 360; height: 360 title: "Union Lotto Game" color: "white" MouseArea { anchors.fill: parent onClicked: { gemini.begin() gemini.stop() //2、調用,在QML中訪問C++的成員函數的形式是<id>.<method> } } Gemini { id: gemini onBegin: doSomething(Gemini.BALL_COLOR_RED) } }
4、C++類的屬性
#ifndef GEMINI_H #define GEMINI_H #include <QObject> #include <QDebug> class Gemini : public QObject { Q_OBJECT Q_ENUMS(BALL_COLOR) Q_PROPERTY(unsigned int ballNumber READ ballNumber WRITE setBallNumber NOTIFY ballNumberChanged) //1、定義,Q_PROPERTY()巨集,用來在QObject派生類中聲明屬性。 public: Gemini() : m_ballColor(BALL_COLOR_YELLOW), m_ballNumber(0) { qDebug() << "Gemini::Gemini() called"; } enum BALL_COLOR { BALL_COLOR_YELLOW, BALL_COLOR_RED, BALL_COLOR_BLUE, BALL_COLOR_ALL }; unsigned int ballNumber() const { return m_ballNumber; } void setBallNumber(const unsigned int &ballNumber) { if(ballNumber != m_ballNumber) { m_ballNumber = ballNumber; emit ballNumberChanged(); } } Q_INVOKABLE void stop() { qDebug() << "Gemini::stop() called"; } signals: void begin(); void ballNumberChanged(); public slots: void doSomething(BALL_COLOR ballColor) { qDebug() << "Gemini::doSomething() called with" << ballColor; if(ballColor != m_ballColor) { m_ballColor = ballColor; qDebug() << "ball color changed"; } } private: BALL_COLOR m_ballColor; unsigned int m_ballNumber; }; #endif
import QtQuick 2.2 import QtQuick.Window 2.1 import Union.Lotto.Gemini 1.0 Window { visible: true width: 360; height: 360 title: "Union Lotto Game" color: "white" MouseArea { anchors.fill: parent onClicked: { gemini.begin() gemini.stop() gemini.ballNumber = 10 //2、調用,Gemini類中的ballNumber屬性可以在QML中訪問、修改,訪問時調用了ballNumber()函數,
// 修改時調用了setBallNumber()函數,同時還發送了一個信號來自動更新這個屬性值。 } } Gemini { id: gemini onBegin: doSomething(Gemini.BALL_COLOR_RED) onBallNumberChanged: console.log("new ball number is", ballNumber) // 10 Component.onCompleted: console.log("default ball number is", ballNumber) // 0 } }
備註:1、參考網址:https://zhuanlan.zhihu.com/p/633999343
2、本文只為個人學習筆記,其他第三方可以任意使用。