• 【QML】QML与C++混合编程,结构体参数互相传递


    1. 方法1:

    C++结构体的每个成员都声明成QML的属性。QML中就可以直接以属性的方式读写。

    .h文件

    typedef struct {
        QString startRecord;
        QString fanSpeed;
        QString scrBrightness;
        QString printerType;
    } ManuSet_t;
    
    class ManuSet : public QObject
    {
        Q_OBJECT
    
        Q_PROPERTY(QString startRecord READ startRecord WRITE setStartRecord NOTIFY startRecordChanged)
        Q_PROPERTY(QString fanSpeed READ fanSpeed WRITE setFanSpeed NOTIFY fanSpeedChanged)
        Q_PROPERTY(QString scrBrightness READ scrBrightness WRITE setScrBrightness NOTIFY scrBrightnessChanged)
        Q_PROPERTY(QString printerType READ printerType WRITE setPrinterType NOTIFY printerTypeChanged)
    
    public:
        QString startRecord();
        void setStartRecord(const QString &para);
    
        QString fanSpeed();
        void setFanSpeed(const QString &para);
    
        QString scrBrightness();
        void setScrBrightness(const QString &para);
    
        QString printerType();
        void setPrinterType(const QString &para);
        
    signals:
        void startRecordChanged();
        void fanSpeedChanged();
        void scrBrightnessChanged();
        void printerTypeChanged();
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36

    .cpp文件中实现

    main.cpp中注册类型

    qmlRegisterType<ManuSet>("com.custom", 1, 0, "ManuSet");
    
    • 1

    qml文件中直接使用

    import com.custom 1.0
    
    ManuSet{
    	id: _ms
    	startRecord: "test"
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    2. 方法2:

    QVariantMap 的方式。

    2.1 C++发送给QML

    结构体

    struct Info
    {
        float tmp;              // 温度
        int day_code;           // 白天天气代码
        QString day_text;       // 白天天气情况
        int night_code;         // 夜间天气代码
        QString night_text;     // 夜间天气情况
    };
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    C++代码

    //在minxCppDemo类中声明和定义
    QVariantMap MixCppDemo::getInfo(Info *m_Info)
    {
        QVariantMap map;
        map.clear();
    
        map.insert("tmp", m_Info->tmp);
        map.insert("day_code", m_Info->day_code);
        map.insert("day_text", m_Info->day_text);
        map.insert("night_code", m_Info->night_code);
        map.insert("night_text", m_Info->night_text);
    
        return map;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    QML代码

    MixCppDemo {
    	id: minxCppDemo
    }
    
    Component.onCompleted: {
    	var textString = ''
    	// 显示天气信息
    	var currentInfo = minxCppDemo.getInfo()
    	textString += '温度:' 	     + currentInfo.tmp.toString() + '\n';
    	textString += '白天天气代码: ' + currentInfo.day_code.toString() + '\n'
    	textString += '白天天气情况: ' + currentInfo.day_text + '\n'
    	textString += '夜间天气代码: ' + currentInfo.night_code.toString() + '\n'
    	textString += '夜间天气情况: ' + currentInfo.night_text + '\n\n'
    	
    	console.log(textString);
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    2.2 QML发送给C++

    结构体

    struct Info
    {
        float tmp;              // 温度
        int day_code;           // 白天天气代码
        QString day_text;       // 白天天气情况
        int night_code;         // 夜间天气代码
        QString night_text;     // 夜间天气情况
    };
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    QML代码

    MixCppDemo {
    	id: minxCppDemo
    }
    
    function test(){
    	var map = {};
    	
    	map.tmp = 25.5;
    	//map["tmp"] = 25.5; //与上面的代码效果一样
    	map.day_code = 1;
    	map.day_text = "2";
    	map.night_code = 3;
    	map.night_text  = "4";
    
    	minxCppDemo.setInfo(map);
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    C++代码

    Info m_Info;
    
    void MixCppDemo::setInfo(const QVariantMap map)
    {
    	m_Info.tmp = map.value("tmp").toString();
    	//...
    
        qDebug() << map.value("tmp").toString();
        qDebug() << map.value("day_code").toString();
        qDebug() << map.value("day_text").toString();
        qDebug() << map.value("night_code").toString();
        qDebug() << map.value("night_text").toString();
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
  • 相关阅读:
    你觉得工作开心重要还是赚钱重要?
    鸿蒙开发实战-手写一个Openharmony投屏工具
    用 AI 编程助手从零生成 3D 智慧校园数据大屏 —— Claude Code 实战全记录
    数据仓库应该用什么方案——数据仓库实施方案概述
    Python——requests模块详解
    为什么MyBatis是Java数据库持久层的明智选择
    STM32看门狗
    【Linux operation 42】Linux 系统的时间
    人工智能知识全面讲解:感知机原理
    Dockerfile COPY的奇怪行为:自动解包一级目录
  • 原文地址:https://blog.csdn.net/yangshuoSB/article/details/134499874