QML学习十四:C++访问QML函数

若该文为原创文章,转载请注明原文出处

记录C++端如何访问QML端的函数,QML端访问C++端函数只需要声明就可以访问,那么C++端要如何访问QML端函数呢?

举个例子,在main.qml里有个函数qmlFunc(),我们希望C++可以访问这个函数返回success的返回值。

 一、获取对象

通过QMetaObject::invokemethod()来调用QML端的函数,具体函数详细请查询QT助手。

 二、完整代码

1、main.qml 

import QtQuick 2.12
import QtQuick.Window 2.12
import QtQuick.Controls 2.12
 
import MyObj 1.0
 
Window {
    id: window
    visible: true
    width: SCREEN_WIDTH
    height: 480
    objectName: window
    title: qsTr("Hello World")
 
    signal qmlSig(int i, string s)

    function qmlFunc(i, s) {
        return "success"
    }

    Button {
        onClicked: {
            //myobj.cppSig(99, "lisi")
            MyObject.func();
        }
    }

//    Connections {
//        target: MyObject
//        function onCppSig(i, s) {
//            qmlSlot(i, s)
//        }
//    }
}

2、main.cpp

#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QQmlContext>
#include "myobject.h"
 
int main(int argc, char *argv[])
{
    QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
 
    QGuiApplication app(argc, argv);
 
    app.setOrganizationName("myApp");
 
    QQmlApplicationEngine engine;
    QQmlContext *context = engine.rootContext();
    context->setContextProperty("SCREEN_WIDTH", 800);
 
//    MyObject obj;
//    context->setContextProperty("MyObject", &obj);
 
//    context->setContextProperty("MyObject", MyObject::getInstance());
 
    qmlRegisterType<MyObject>("MyObj", 1, 0, "MyObject");

    qmlRegisterSingletonInstance("MyObj", 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);
 
    // engine 加载完成后 load以后
    auto list = engine.rootObjects();
    auto window = list.first();
    QObject::connect(window, SIGNAL(qmlSig(int, QString)),
                     MyObject::getInstance(), SLOT(cppSlot(int, QString)));

    QObject::connect(MyObject::getInstance(), SIGNAL(cppSig(int, QString)),
                     window, SLOT(qmlSlot(int, QString)));

    QVariant res;
    QVariant arg_1 = 123;
    QVariant arg_2 = "zhangsan"
    QMetaObject::invokeMethod(window, "qmlFunc",
                              Q_RETURN_ARG(QVariant, res),
                              Q_ARG(QVariant, arg_1),
                              Q_ARG(QVariant, arg_2));

    qDebug() << "res = " << res;
    
 
    return app.exec();
}

通过代码可以看出,结果打印了返回值,返回类开QVariant。

3、结果

三、总结

在实际项目中,目前为上,C++端调用QML函数很少用到,用到最多的还是单便模式,即QML端访问C++端。

如有侵权,请及时联系博主删除,VX:18750903063

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

殷忆枫

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值