【极客技术传送门】 : https://blog.csdn.net/Engineer_LU/article/details/135149485
Qml与C++通信有以下思路
C++代码如下 :
int main(int argc, char *argv[])
{
QGuiApplication app(argc, argv);
QQmlApplicationEngine engine;
engine.rootContext()->setContextProperty("HEIGHT",500);
engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
if (engine.rootObjects().isEmpty())
return -1;
return app.exec();
}
Qml代码如下 :
Window {
id: window
visible: true
width: 300
height: HEIGHT
title: qsTr("Hello World")
Component.onCompleted: {
console.log(window.width)
}
Button{
width: 100
height: 100
background: {
color:"black"
}
}
}
小结 :
C++代码如下 :
#ifndef MYOBJECT_H
#define MYOBJECT_H
#include <QObject>
#include <QtQml>
class MyObject : public QObject
{
Q_OBJECT
Q_PROPERTY(int value READ getValue WRITE setValue NOTIFY valueChanged)
Q_PROPERTY(QString str READ getStr WRITE setStr NOTIFY strChanged)
public:
explicit MyObject(QObject *parent = nullptr);
public:
void setValue(int newValue);
int getValue();
void setStr(QString newStr);
QString getStr();
signals:
void valueChanged();
void strChanged();
private:
int m_value;
QString m_str;
};
#endif // MYOBJECT_H
#include "MyObject.h"
MyObject::MyObject(QObject *parent) : QObject(parent)
{
}
void MyObject::setValue(int newValue)
{
if(newValue == m_value)
return;
m_value = newValue;
emit valueChanged();
}
int MyObject::getValue()
{
return m_value;
}
void MyObject::setStr(QString newStr)
{
if(newStr == m_str)
return;
m_str = newStr;
emit strChanged();
}
QString MyObject::getStr()
{
return m_str;
}
int main(int argc, char *argv[])
{
QGuiApplication app(argc, argv);
QQmlApplicationEngine engine;
qmlRegisterType<MyObject>("MyObject", 1, 0, "MyObject");
engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
if (engine.rootObjects().isEmpty())
return -1;
return app.exec();
}
Qml代码如下 :
import MyObject 1.0
Window {
id: window
visible: true
width: 400
height: 500
title: qsTr("Hello World")
MyObject{
value: 10
str: "zhangsan"
onValueChanged: {
}
onStrChanged: {
}
Component.onCompleted: {
console.log(value,str)
}
}
}
小结 :
C++代码如下 :
#ifndef MYOBJECT_H
#define MYOBJECT_H
#include <QObject>
#include <QtQml>
class MyObject : public QObject
{
Q_OBJECT
Q_PROPERTY(int value READ getValue WRITE setValue NOTIFY valueChanged)
Q_PROPERTY(QString str READ getStr WRITE setStr NOTIFY strChanged)
public:
explicit MyObject(QObject *parent = nullptr);
public:
Q_INVOKABLE void printMsg();
void setValue(int newValue);
int getValue();
void setStr(QString newStr);
QString getStr();
signals:
void valueChanged();
void strChanged();
private:
int m_value;
QString m_str;
};
#endif // MYOBJECT_H
#include "MyObject.h"
MyObject::MyObject(QObject *parent) : QObject(parent)
{
}
void MyObject::setValue(int newValue)
{
if(newValue == m_value)
return;
m_value = newValue;
emit valueChanged();
}
int MyObject::getValue()
{
return m_value;
}
void MyObject::setStr(QString newStr)
{
if(newStr == m_str)
return;
m_str = newStr;
emit strChanged();
}
QString MyObject::getStr()
{
return m_str;
}
int main(int argc, char *argv[])
{
QGuiApplication app(argc, argv);
QQmlApplicationEngine engine;
qmlRegisterType<MyObject>("MyObject", 1, 0, "MyObject");
engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
if (engine.rootObjects().isEmpty())
return -1;
return app.exec();
}
Qml代码如下 :
import MyObject 1.0
Window {
id: window
visible: true
width: 400
height: 500
title: qsTr("Hello World")
MyObject{
id: obj
value: 10
str: "zhangsan"
}
Button{
width: 50
height: 50
background: Rectangle{
color:"red"
}
onClicked: {
obj.printMsg()
}
}
}
小结 :
C++的代码如下
signals:
void sigMsg(int value,QString name);//在C++中调用 emit sigMsg(xxx,"xxx");
Qml的代码如下
MyObject{ id : obj
}
Connections{ //信号-槽连接方式一
target: obj
function onSigMsg(i,s){
console.log("/*-----------收到信号----------*/")
}
}
小结 :
C++的代码如下
public slots:
void slotMsg(int value,QString name);
//实现
void MyObject::slotMsg(int value,QString name)
{
qDebug()<<__FUNCTION__<<" value = "<<value<<" name ="<<name;
}
Qml的代码如下
Window {
id: window
visible: true
width: 400
height: 500
title: qsTr("Hello World")
signal sendMsg(int value,string name) //增加信号
MyObject{
id: obj
value: 10
str: "zhangsan"
}
Button{
width: 50
height: 50
background: Rectangle{
color:"red"
}
onClicked: {
sendMsg(2,"zhangsan")
}
}
Component.onCompleted: { //信号-槽连接方式二
sendMsg.connect(obj.slotMsg)
}
}
小结 :
Qml的代码如下
Window {
id: window
objectName: "window"
visible: true
width: 400
height: 500
title: qsTr("Hello World")
}
C++的代码如下
QQmlApplicationEngine engine;
qmlRegisterType<MyObject>("MyObject", 1, 0, "MyObject");
engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
if (engine.rootObjects().isEmpty())
return -1;
auto objs = engine.rootObjects();
qDebug()<<objs.first()->objectName();
MyObject *myobj = new MyObject();
auto objs = engine.rootObjects();
auto window = objs.first();
qDebug()<<objs.first()->objectName();
QObject::connect(window,SIGNAL(sendMsg(int,QString)),
myobj,SLOT(slotMsg(int,QString)));
小结 :
Qml的代码如下
function test(value,name){ //供C++端调用的函数
console.log("test ",value,name)
}
C++的代码如下
QVariant ret;
QVariant arg1 = 123;
QVariant arg2 = "zhangsan";
QMetaObject::invokeMethod(window,"test",Q_RETURN_ARG(QVariant,ret)
,Q_ARG(QVariant,arg1)
,Q_ARG(QVariant,arg2));
小结 :
以上描述了C++与QML数据交互的方式,大家可以根据以上方式扩展,模拟MVVM的模式代入框架实现简洁高效的前后端架构,谢谢观看。
技术交流QQ群 : 745662457
群内专注 - 问题答疑,项目外包,技术研究