QML开发过程中除了UI开发剩下的就是QML与C++互调方式了
最近开始学习Qt的QML开发,主要原因是原有的qwidget开发效率感觉实在不高,所以学一下QML好应用到以后的项目中,提高客户端开发效率。
为每个功能都去写函数啥的,我觉得挺麻烦的。所以我认为统一调用入口,以后开发过程更加简单,也更好维护。
QML调用C++统一用
callCppFunc(qstring funcName,QMap<qstring,qvariant> parDdata)
C++调用QML里面的函数用—信号方式
emit sig_callQml(qstring funcName,QMap<qstring,qvariant> parDdata)
下面是使用方式
首先是头文件myclass.h文件
#include <QObject>
#include <QVariantMap>
class Myclass : public QObject
{
Q_OBJECT
public:
explicit Myclass(QObject *parent = nullptr);
signals:
void sig_callQml(QString funcStr,QVariantMap data);
public:
Q_INVOKABLE QString callCppFunc(QString funcStr,QVariantMap data);
private:
QString m_str="";
};
myclass.cpp
#include "myclass.h"
#include<QDebug>
Myclass::Myclass(QObject *parent) : QObject(parent)
{
}
void Myclass::slot_tst(QString _str)
{
m_str = _str;
qDe