C++ 中 调用 QML 对象函数
import QtQuick 2.12 import QtQuick.Window 2.12 import QtQuick.Controls 2.5 Window { width: 640 height: 480 visible: true title: qsTr("QML与CPP") Label{ objectName: "qmlLab" text: "QML Label" font.pointSize: 16 //在cpp中调用此函数 function getText(info) { return text + info } } }
- main.cpp
#include <QGuiApplication> #include <QQmlApplicationEngine> #include <QMetaObject> //元对象系统 #include <QDebug> 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; 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 getText() 函数 auto root = engine.rootObjects(); auto labQml = root.first()->findChild<QObject*>("qmlLab"); QVariant ret; //通过元系统调用函数返回值 QMetaObject::invokeMethod(labQml, "getText", Q_RETURN_ARG(QVariant, ret), Q_ARG(QVariant, "DEMO")); qDebug() << ret.toString(); return app.exec(); }
- 输出:
QML 中 调用 C++ 对象函数
- 目录结构:
- person.h
#ifndef PERSON_H #define PERSON_H #include <QObject> #include <QString> class Person : public QObject { Q_OBJECT public: using QObject::QObject; Person(QString name, qint32 age) noexcept; Q_INVOKABLE void printInfo()const noexcept; //qml中调用 private: QString m_strName; qint32 m_iAge; }; #endif // PERSON_H
- person.cpp
#include "Person.h" #include <QDebug> Person::Person(QString name, qint32 age) noexcept: m_strName(qMove(name)),m_iAge(age) { } void Person::printInfo() const noexcept { qDebug()<< __FILE__ << ":"<< __FUNCTION__ << "name: " << m_strName << "age: " << m_iAge; //qml中调用 }
- main.cpp
#include "./Person.h" #include <QGuiApplication> #include <QQmlApplicationEngine> #include <QMetaObject> //元对象系统 #include <QDebug> 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); Person person("Miao", 72); //注册到qml qmlRegisterSingletonInstance("MiaoModel", 1, 0, "Person", &person); QQmlApplicationEngine engine; 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
import QtQuick 2.12 import QtQuick.Window 2.12 import QtQuick.Controls 2.5 import MiaoModel 1.0 Window { width: 640 height: 480 visible: true title: qsTr("QML与CPP") Button{ text: "click" onClicked: { Person.printInfo(); } } }
- 输出:
另一种注册方式
- 直接注入到引擎,在qml中不需要导入模块,并且有提示
#include "./Person.h" #include <QGuiApplication> #include <QQmlApplicationEngine> #include <QMetaObject> //元对象系统 #include <QQmlContext> #include <QDebug> 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; 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); Person person("Miao", 72); auto context = engine.rootContext(); context->setContextProperty("Person", &person); engine.load(url); return app.exec(); }