C++ 与 QML 相互调用对象函数

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

 

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值