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

1. 方法1:

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

.h文件

c 复制代码
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();
}

.cpp文件中实现

main.cpp中注册类型

c 复制代码
qmlRegisterType<ManuSet>("com.custom", 1, 0, "ManuSet");

qml文件中直接使用

c 复制代码
import com.custom 1.0

ManuSet{
	id: _ms
	startRecord: "test"
}

2. 方法2:

QVariantMap 的方式。

2.1 C++发送给QML

结构体

c 复制代码
struct Info
{
    float tmp;              // 温度
    int day_code;           // 白天天气代码
    QString day_text;       // 白天天气情况
    int night_code;         // 夜间天气代码
    QString night_text;     // 夜间天气情况
};

C++代码

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;
}

QML代码

c 复制代码
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);
}

2.2 QML发送给C++

结构体

c 复制代码
struct Info
{
    float tmp;              // 温度
    int day_code;           // 白天天气代码
    QString day_text;       // 白天天气情况
    int night_code;         // 夜间天气代码
    QString night_text;     // 夜间天气情况
};

QML代码

c 复制代码
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);
}

C++代码

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();
}
相关推荐
ShineWinsu5 分钟前
对于Linux:基于UDP实现简单聊天室功能
linux·c++·面试·udp·笔试·进程·简单聊天室
chase_my_dream16 分钟前
2D-SLAM 真实数据处理与多传感器工程落地:时间同步、异常过滤、标定对齐和系统调试
c++·人工智能·2d-slam
c238561 小时前
《序列 DP:C++ 中的“最长”套路与编辑距离》
c++·算法·动态规划
鱼子星_1 小时前
【C++】类和对象(下)——初始化列表,类型转换,static成员,友元,内部类,匿名对象,编译器的优化拓展
c语言·c++·笔记
王老师青少年编程9 小时前
2025年【江苏“信息与未来”编程思维】真题及题解(T2:坐标变换)
c++·题解·真题·坐标变换·编程思维·江苏·信息与未来
2023自学中13 小时前
C++ 内存追踪器
linux·c++
杜子不疼.15 小时前
【Qt初识】信号槽(三):机制意义、断开连接与 Lambda 表达式
开发语言·数据库·qt
DogDaoDao17 小时前
【GitHub】 LLVM Project 深度解析:现代编译器基础设施的基石
java·c++·python·程序员·github·编译器·llvm
KuaCpp17 小时前
C++进程(下)
c++
杜子不疼.17 小时前
【Qt初识】信号槽(二):自定义信号函数与槽函数
开发语言·qt