首页 > 编程语言 >C++和QML混合编程

C++和QML混合编程

时间:2023-12-14 16:48:17浏览次数:26  
标签:编程 C++ Gemini QML import include gemini

  • 一、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、本文只为个人学习笔记,其他第三方可以任意使用。

 

标签:编程,C++,Gemini,QML,import,include,gemini
From: https://www.cnblogs.com/wxzhrj/p/17899601.html

相关文章

  • C++(c_str())
    在C++中,c_str()是std::string类的成员函数,用于返回一个指向以null结尾的字符数组(C风格字符串)的指针。这个函数主要用于将C++标准字符串转换为C风格字符串,以便与接受C风格字符串参数的函数进行交互。函数签名:constchar*c_str()constnoexcept;示例:#include<......
  • Windows上的c/c++编译工具
    Windows上的编译工具有很多种,以下是一些常见的选择:VisualStudio:这是微软开发的一款IDE,支持多种编程语言,包括C++。它提供了丰富的功能,如代码编译、调试、版本控制等。VisualStudio还包含一些强大的工具,如代码智能感知和代码重构工具,可以帮助开发人员提高开发效率。   有......
  • C++学习笔记十一:数据类型的转换
    一个表达式里的所有变量应该具有相同的类型。上溢和下溢(overflowandunderflow):1.隐式转换(implicitly):编译器自动进行。总是把占用内存小的数据类型转化为占用大的数据类型。int类型转换为doubledoubleprice{45.6};intunits{10};autototal_price=price*un......
  • Amazon CodeWhisperer:AI 编程助手
    文章作者:prigioni1.什么是AmazonCodeWhisperer?AmazonCodeWhisperer能够理解以自然语言(英语)编写的注释,并能实时生成多条代码建议,以此提高开发人员生产力。该服务可以直接在集成开发环境(IDE)的代码编辑器中给出关于整个功能和逻辑代码块(通常包含多达10-15行代码)的建议。生成......
  • C++(size_t)
    size_t是C++中的一种数据类型,通常用于表示对象的大小或元素的数量。它是一种无符号整数类型,具体的大小依赖于编译器和系统,但通常被设计为能够表示对象的最大可能大小。特点和用途:无符号整数类型:size_t是一种无符号整数类型,因此它只能表示非负的整数值。与sizeof运算......
  • C++ Qt开发:ComboBox下拉组合框组件
    Qt是一个跨平台C++图形界面开发库,利用Qt可以快速开发跨平台窗体应用程序,在Qt中我们可以通过拖拽的方式将不同组件放到指定的位置,实现图形化开发极大的方便了开发效率,本章将重点介绍ComboBox下拉组合框组件的常用方法及灵活运用。在Qt中,ComboBox(组合框)是一种常用的用户界面控件,它......
  • C++( std::string::npos)
    std::string::npos是std::string类中的一个静态常量,通常用于表示字符串操作中的特殊值,表示在字符串中未找到匹配的位置。npos是size_t类型的常量,其值在不同平台上可能有所不同,但通常是一个非常大的正整数。在std::string的成员函数中,npos用于表示一个无效或未找到的位置......
  • 利用Docker和CLion在Mac优雅地开发和调试Linux C++程序
    利用Docker和CLion在Mac优雅地开发和调试LinuxC++程序starrymarin计算机主业,间断性健身爱好者,摩托、金融入门​关注他 27人赞同了该文章最近在做一些新的东西,所以学习了一些新的东西,也对旧知识加强了很多,所以终于有东西可以记录一下了。今天先更......
  • Qt/C++视频监控安卓版/多通道显示视频画面/录像存储/视频播放安卓版/ffmpeg安卓
    一、前言随着监控行业的发展,越来越多的用户场景是需要在手机上查看监控,而之前主要的监控系统都是在PC端,毕竟PC端屏幕大,能够看到的画面多,解码性能也强劲。早期的手机估计性能弱鸡,而现在的手机性能不是一般的牛,甚至超越了PC机的性能,所以手机上查看多路监控也就有了硬件基础前提。对......
  • C++中的图像处理与变换总结
    个人总结图像加法去噪是对同一场景的多幅图像求平均值,以降低加性随机噪声。随机噪声在不同的图像中是独立的,而场景信息是相同的。因此,通过将多幅图像相加并求平均,可以使场景信息保持不变,而噪声的影响则会减小。这是因为随机噪声的期望值为零,所以多幅图像的平均值会使噪声趋向于零......