C++和QML混合編程

来源:https://www.cnblogs.com/wxzhrj/archive/2023/12/14/17899601.html
-Advertisement-
Play Games

在Qt中,ComboBox(組合框)是一種常用的用戶界面控制項,它提供了一個下拉列表,允許用戶從預定義的選項中選擇一個。該組件提供了一種方便的方式讓用戶從預定義的選項中進行選擇,一般來說`ComboBox`會以按鈕的形式顯示在界面上,用戶點擊按鈕後,會彈出一個下拉列表,其中包含預定義的選項。當然`Co... ...


  • 一、QML訪問C++方法

  1. Qt元對象系統中註冊C++類,在QML中實例化、訪問。
  2. C++中實例化並設置為QML上下文屬性,在QML中直接使用。

           比較:方法1可以使C++類在QML中作為一個數據類型,例如函數參數類型或屬性類型,也可以使用其枚舉類型、單例等,功能更強大。

  • 二、QML訪問C++條件

  1. 派生自QObject類或QObject類的子類
  2. 使用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、本文只為個人學習筆記,其他第三方可以任意使用。

 


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • HarmonyOS 實戰小項目開發(一) 日常逼逼叨 在經過一周多的Harmonyos 開發基礎知識的學習後,自己通過對於Harmonyos基礎知識的學習之後,結合自己的一些想法,獨自完成了利用Arkts佈局的Harmonyos 項目,在此將整個過程與各位共用出來,如有一些錯誤,希望觀眾老爺們批評指 ...
  • 這裡給大家分享我在網上總結出來的一些知識,希望對大家有所幫助 DOM小練習 彈幕 電梯導航 倒計時 隨機點名 購物放大鏡 1.彈幕 效果預覽 功能:輸入彈幕內容,按下回車顯示一條彈幕(彈幕顏色、字體隨機生成) 思路:設置按鈕抬起事件,在事件中判斷如果按下的是回車鍵則將輸入框中替換掉敏感詞的數據追加到 ...
  • 在 Chrome 擴展開發中,manifest 文件是項目的核心,其中 "content_scripts","background","permissions" 配置項又至關重要! ...
  • 即時設計平臺是一個即時搭建c端樓層的開發平臺,支持通過導入relay設計稿url完成Ui2Code,在此基礎上完成前端可視化搭建,同時支持通過ChatGPT完成一句話需求,搭建後的樓層自動同步ihub樓層市場,提供到通天塔、哥倫布等搭建平臺使用。 ...
  • 說明:任意文件上傳漏洞,很多PHP開發者也會做一些簡單的防護,但是這個防護有被繞過的可能。 原生漏洞PHP示例代碼: $file = $_FILES['file'] ?? []; //檢測文件類型 $allow_mime = ['image/jpg', 'image/jpeg', 'image/pn ...
  • 近年來,通知功能已經成為許多應用程式中突出的特性。構建一個能每天發送數百萬通知的可擴展系統絕非易事。這正是為什麼我覺得有必要記錄我在這方面踩坑之路。也叫用戶觸達系統。 完成這項任務要求對通知生態系統有深刻的理解,否則需求很容易變得模糊和不明確。 1 瞭解通知系統並確定設計範圍 通知是用於向用戶提供重 ...
  • C 語言中的註釋 C語言中可以使用註釋來解釋代碼並使其更具可讀性。它還可以在測試替代代碼時防止執行。 單行註釋 單行註釋以兩個斜杠 (//) 開頭。 // 和行末之間的任何文本都會被編譯器忽略(不會被執行)。 此示例在代碼行之前使用單行註釋: // 這是一個註釋 printf("Hello Worl ...
  • Selenium系列知識點整理 https://www.cnblogs.com/yoyoketang/ 本文摘錄於‘上海-悠悠’的博客,網址如上 新手學習selenium路線圖(老司機親手繪製)-學前篇 學習selenium主要分六個階段,自己在哪個層級,可以對號入座下。第 一階段:幼兒園 1.選語 ...
一周排行
    -Advertisement-
    Play Games
  • 移動開發(一):使用.NET MAUI開發第一個安卓APP 對於工作多年的C#程式員來說,近來想嘗試開發一款安卓APP,考慮了很久最終選擇使用.NET MAUI這個微軟官方的框架來嘗試體驗開發安卓APP,畢竟是使用Visual Studio開發工具,使用起來也比較的順手,結合微軟官方的教程進行了安卓 ...
  • 前言 QuestPDF 是一個開源 .NET 庫,用於生成 PDF 文檔。使用了C# Fluent API方式可簡化開發、減少錯誤並提高工作效率。利用它可以輕鬆生成 PDF 報告、發票、導出文件等。 項目介紹 QuestPDF 是一個革命性的開源 .NET 庫,它徹底改變了我們生成 PDF 文檔的方 ...
  • 項目地址 項目後端地址: https://github.com/ZyPLJ/ZYTteeHole 項目前端頁面地址: ZyPLJ/TreeHoleVue (github.com) https://github.com/ZyPLJ/TreeHoleVue 目前項目測試訪問地址: http://tree ...
  • 話不多說,直接開乾 一.下載 1.官方鏈接下載: https://www.microsoft.com/zh-cn/sql-server/sql-server-downloads 2.在下載目錄中找到下麵這個小的安裝包 SQL2022-SSEI-Dev.exe,運行開始下載SQL server; 二. ...
  • 前言 隨著物聯網(IoT)技術的迅猛發展,MQTT(消息隊列遙測傳輸)協議憑藉其輕量級和高效性,已成為眾多物聯網應用的首選通信標準。 MQTTnet 作為一個高性能的 .NET 開源庫,為 .NET 平臺上的 MQTT 客戶端與伺服器開發提供了強大的支持。 本文將全面介紹 MQTTnet 的核心功能 ...
  • Serilog支持多種接收器用於日誌存儲,增強器用於添加屬性,LogContext管理動態屬性,支持多種輸出格式包括純文本、JSON及ExpressionTemplate。還提供了自定義格式化選項,適用於不同需求。 ...
  • 目錄簡介獲取 HTML 文檔解析 HTML 文檔測試參考文章 簡介 動態內容網站使用 JavaScript 腳本動態檢索和渲染數據,爬取信息時需要模擬瀏覽器行為,否則獲取到的源碼基本是空的。 本文使用的爬取步驟如下: 使用 Selenium 獲取渲染後的 HTML 文檔 使用 HtmlAgility ...
  • 1.前言 什麼是熱更新 游戲或者軟體更新時,無需重新下載客戶端進行安裝,而是在應用程式啟動的情況下,在內部進行資源或者代碼更新 Unity目前常用熱更新解決方案 HybridCLR,Xlua,ILRuntime等 Unity目前常用資源管理解決方案 AssetBundles,Addressable, ...
  • 本文章主要是在C# ASP.NET Core Web API框架實現向手機發送驗證碼簡訊功能。這裡我選擇是一個互億無線簡訊驗證碼平臺,其實像阿裡雲,騰訊雲上面也可以。 首先我們先去 互億無線 https://www.ihuyi.com/api/sms.html 去註冊一個賬號 註冊完成賬號後,它會送 ...
  • 通過以下方式可以高效,並保證數據同步的可靠性 1.API設計 使用RESTful設計,確保API端點明確,並使用適當的HTTP方法(如POST用於創建,PUT用於更新)。 設計清晰的請求和響應模型,以確保客戶端能夠理解預期格式。 2.數據驗證 在伺服器端進行嚴格的數據驗證,確保接收到的數據符合預期格 ...