目录
1 QML获取C++的变量值
2 QML获取C++创建的自定义对象
3 QML发送信号绑定C++端的槽
4 C++端发送信号绑定qml端槽
5 C++调用QML端函数
1 QML获取C++的变量值
QQmlApplicationEngine engine;
全局对象
上下文属性
QQmlApplicationEngine engine;
QQmlContext *context1 = engine.rootContext();
context1->setContextProperty("test",200);
在qml中可全局直接使用test
2 QML获取C++创建的自定义对象
光标放在成员变量m_iValue和m_sString后面 Alt + Enter 选择第一个可自动生成函数
int m_iValue;
QString m_sString;
myobject.h
#ifndef MYOBJECT_H
#define MYOBJECT_H
#include <QObject>
class MyObject : public QObject
{
Q_OBJECT
public:
explicit MyObject(QObject *parent = nullptr);
int iValue() const;
void setIValue(int newIValue);
const QString &sString() const;
void setSString(const QString &newSString);
signals:
void iValueChanged();
void sStringChanged();
private:
int m_iValue;
QString m_sString;
Q_PROPERTY(int iValue READ iValue WRITE setIValue NOTIFY iValueChanged)
Q_PROPERTY(QString sString READ sString WRITE setSString NOTIFY sStringChanged)
};
#endif // MYOBJECT_H
myobject.c
#include "myobject.h"
MyObject::MyObject(QObject *parent)
: QObject{parent}
{
}
int MyObject::iValue() const
{
return m_iValue;
}
void MyObject::setIValue(int newIValue)
{
if (m_iValue == newIValue)
return;
m_iValue = newIValue;
emit iValueChanged();
}
const QString &MyObject::sString() const
{
return m_sString;
}
void MyObject::setSString(const QString &newSString)
{
if (m_sString == newSString)
return;
m_sString = newSString;
emit sStringChanged();
}
mian.c
注册main.qml 中 import testObj 1.0使用
qmlRegisterType<MyObject>("testObj",1,0,"MyObject");
main.qml
import QtQuick 2.15
import QtQuick.Window 2.15
import QtQuick.Controls 2.5
import testObj 1.0
Window {
width: 640
height: 480
visible: true
title: qsTr("Hello World")
MyObject {
iValue: 20
sString: "test"
}
}
使用c++端的函数时需要在函数前面加上 Q_INVOKABLE
比如 Q_INVOKABLE void myFunction();
3 QML发送信号绑定C++端的槽
在mian.c先注册 qmlRegisterType<MyObject>("testObj",1,0,"MyObject");
C++ 写一个公有槽
public slots:
void qml_slot(QString str);
qml端通过按钮发送信号
signal qml_signal(string str)
Button {
onClicked: {
qml_signal("qml send signal test");
}
}
绑定信号和槽函数
Component.onCompleted: {
qml_signal.connect(myobj.qml_slot)
}
import QtQuick 2.15
import QtQuick.Window 2.15
import QtQuick.Controls 2.5
import testObj 1.0
Window {
id: window
width: 640
height: 480
visible: true
title: qsTr("Hello World")
MyObject {
id: myobj
iValue: 20
sString: "test"
}
signal qml_signal(string str)
Button {
onClicked: {
qml_signal("qml send signal test");
}
}
//方法一
// Connections {
// target: window
// function onQml_signal(str){
// myobj.qml_slot(str)
// }
// }
//方法二
Component.onCompleted: {
qml_signal.connect(myobj.qml_slot)
}
//方法三 mian.c 中engine load后
//auto obj_list = engine.rootObjects();
//auto window = list.first();
//connect(window,SIGNAL(qml_signal(QString)),your function ptr,SLOT(qml_slot(QString)));
}
4 C++端发送信号绑定qml端槽
myobject.h
信号 void signal_Cpp(QString str); 通过函数
Q_INVOKABLE void myFunction();发送
#ifndef MYOBJECT_H
#define MYOBJECT_H
#include <QObject>
class MyObject : public QObject
{
Q_OBJECT
public:
explicit MyObject(QObject *parent = nullptr);
int iValue() const;
void setIValue(int newIValue);
const QString &sString() const;
void setSString(const QString &newSString);
Q_INVOKABLE void myFunction();
static MyObject* getInstance();
public slots:
void qml_slot(QString str);
signals:
void iValueChanged();
void sStringChanged();
void signal_Cpp(QString str);
private:
int m_iValue;
QString m_sString;
Q_PROPERTY(int iValue READ iValue WRITE setIValue NOTIFY iValueChanged)
Q_PROPERTY(QString sString READ sString WRITE setSString NOTIFY sStringChanged)
};
#endif // MYOBJECT_H
myobject.c
#include "myobject.h"
#include <QDebug>
MyObject::MyObject(QObject *parent)
: QObject{parent}
{
}
int MyObject::iValue() const
{
return m_iValue;
}
void MyObject::setIValue(int newIValue)
{
if (m_iValue == newIValue)
return;
m_iValue = newIValue;
emit iValueChanged();
}
const QString &MyObject::sString() const
{
return m_sString;
}
void MyObject::setSString(const QString &newSString)
{
if (m_sString == newSString)
return;
m_sString = newSString;
emit sStringChanged();
}
void MyObject::myFunction()
{
emit signal_Cpp("signal_Cpp myFunction!");
}
MyObject *MyObject::getInstance()
{
static MyObject* obj = new MyObject();
return obj;
}
void MyObject::qml_slot(QString str)
{
qDebug()<<"qml_slot"<<str;
}
mian.c
注册单例类
qmlRegisterSingletonInstance("testObj",1,0,"MyObject",MyObject::getInstance());
#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QQmlContext>
#include <QtQml/qqml.h>
#include "myobject.h"
int main(int argc, char *argv[])
{
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
#endif
QGuiApplication app(argc, argv);
QQmlApplicationEngine engine;
QQmlContext *context1 = engine.rootContext();
context1->setContextProperty("test",200);
//qmlRegisterType<MyObject>("testObj",1,0,"MyObject");
qmlRegisterSingletonInstance("testObj",1,0,"MyObject",MyObject::getInstance());
const QUrl url(QStringLiteral("qrc:/main.qml"));
QObject::connect(&engine, &QQmlApplicationEngine::objectCreated,
&app, [url](QObject *obj, const QUrl &objUrl) {
if (!obj && url == objUrl)
QCoreApplication::exit(-1);
}, Qt::QueuedConnection);
engine.load(url);
return app.exec();
}
main.qml
C++端发送信号
Button {
onClicked: {
//C++发送信号
MyObject.myFunction()
}
}
连接QML端槽函数
Connections {
target: MyObject
function onSignal_Cpp(str) {
//qml槽函数
qmlSolt(str)
}
}
import QtQuick 2.15
import QtQuick.Window 2.15
import QtQuick.Controls 2.5
import testObj 1.0
Window {
id: window
width: 640
height: 480
visible: true
title: qsTr("Hello World")
// MyObject {
// id: myobj
// iValue: 20
// sString: "test"
// }
function qmlSolt(str) {
console.log(str)
}
signal qml_signal(string str)
Button {
onClicked: {
//C++发送信号
MyObject.myFunction()
}
}
Connections {
target: MyObject
function onSignal_Cpp(str) {
//qml槽函数
qmlSolt(str)
}
}
}
5 C++调用QML端函数
//main.c load后获取qml对象
auto obj_list = engine.rootObjects();
auto window = obj_list.first();
//调用QML函数
QMetaObject::invokeMethod(window,"qmlFunction",
Q_RETURN_ARG(QVariant,res),
Q_ARG(QVariant,arg1),
Q_ARG(QVariant,arg2)
);
mian.qml文章来源:https://www.toymoban.com/news/detail-650476.html
import QtQuick 2.15
import QtQuick.Window 2.15
import QtQuick.Controls 2.5
import testObj 1.0
Window {
id: window
width: 640
height: 480
visible: true
title: qsTr("Hello World")
function qmlFunction(arg1,arg2) {
return arg1 + arg2;
}
}
mian.c文章来源地址https://www.toymoban.com/news/detail-650476.html
#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QQmlContext>
#include <QtQml/qqml.h>
#include "myobject.h"
int main(int argc, char *argv[])
{
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
#endif
QGuiApplication app(argc, argv);
QQmlApplicationEngine engine;
QQmlContext *context1 = engine.rootContext();
context1->setContextProperty("test",200);
qmlRegisterType<MyObject>("testObj",1,0,"MyObject");
//qmlRegisterSingletonInstance("testObj",1,0,"MyObject",MyObject::getInstance());
const QUrl url(QStringLiteral("qrc:/main.qml"));
QObject::connect(&engine, &QQmlApplicationEngine::objectCreated,
&app, [url](QObject *obj, const QUrl &objUrl) {
if (!obj && url == objUrl)
QCoreApplication::exit(-1);
}, Qt::QueuedConnection);
engine.load(url);
//获取qml对象
auto obj_list = engine.rootObjects();
auto window = obj_list.first();
QVariant res;
QVariant arg1 = "arg1";
QVariant arg2 = "arg2";
//调用QML函数
QMetaObject::invokeMethod(window,"qmlFunction",
Q_RETURN_ARG(QVariant,res),
Q_ARG(QVariant,arg1),
Q_ARG(QVariant,arg2)
);
qDebug()<<res;
return app.exec();
}
到了这里,关于QML与C++交互的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!