C++ 与 QML 进行交互
  JCsIkAITQy58 2023年11月02日 66 0

C++调用QML中的函数

//main.cpp
#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QDebug>

int main(int argc, char *argv[])
{
    QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);

    QGuiApplication app(argc, argv);

    QQmlApplicationEngine engine;
    engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
    if (engine.rootObjects().isEmpty())
        return -1;

    QObject *object = engine.rootObjects().first();
    QObject *rect = object->findChild<QObject*>("rect");
    if(object){
        QVariant sum;
        QMetaObject::invokeMethod(object, "myAdd", Q_RETURN_ARG(QVariant, sum), Q_ARG(QVariant, 1), Q_ARG(QVariant, 2));
        if(sum.isValid()){
            qDebug() << "sum : " << sum.toDouble();
        }
    }

    if(rect){
        QMetaObject::invokeMethod(rect, "myPrint");
    }

    return app.exec();
}
import QtQuick 2.9
import QtQuick.Window 2.2

Window {

    function myAdd(left, right){
        return left + right;
    }

    visible: true
    width: 640
    height: 480
    title: qsTr("Hello World")
    Rectangle{
        id: rect
        objectName: "rect"

        function myPrint(){
            console.log(rect.width + "x" + rect.height + "rectangle");
        }

        width: 40
        height: 40
        color: "red"
        anchors.centerIn: parent
    }
}

这就是最简单的 C++ 调用 qml的函数。 **注意 : ** 如果是按照正常的 javaScript函数的写法,因为 JavaScript是弱类型语言。所有在QT中参数包括返回值的类型都要使用QVariant来代替。而且如果要获取返回值在 QMetaObject::invokeMethod 一定要把 Q_RETURN_ARG写在第一个Q_ARG参数的前方。如果想要调用的 qml 的函数不是在根 object 中,那么还要在qml的对象中设置objectName属性。然后QT C++使用 findChild找到该 对象后然后在进行调用。

C++中链接qml中的信号或者槽

//MyTest.h
#ifndef MYTEST_H
#define MYTEST_H

#include <QObject>

class MyTest : public QObject
{
    Q_OBJECT
public:
    explicit MyTest(QObject *parent = nullptr);

signals:

public slots:
    void slotClick();
};

#endif // MYTEST_H
//MyTest.cpp
#include "MyTest.h"
#include <QDebug>

MyTest::MyTest(QObject *parent) : QObject(parent)
{

}

void MyTest::slotClick()
{
    qDebug() << QStringLiteral("按钮点击了");
}
//main.qml
import QtQuick 2.9
import QtQuick.Window 2.2
import QtQuick.Controls 2.2

Window {

    function myAdd(left, right){
        return left + right;
    }

    visible: true
    width: 640
    height: 480
    title: qsTr("Hello World")
    Button{
        id: button
        objectName: "button"
        width: 40
        height: 20
        text: "按钮"
    }
    Rectangle{
        id: rect
        objectName: "rect"

        function myPrint(){
            console.log(rect.width + "x" + rect.height + "rectangle");
        }

        width: 40
        height: 40
        color: "red"
        anchors.centerIn: parent
    }
}
//main.cpp
#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QDebug>
#include "MyTest.h"

int main(int argc, char *argv[])
{
    QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);

    QGuiApplication app(argc, argv);

    QQmlApplicationEngine engine;
    engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
    if (engine.rootObjects().isEmpty())
        return -1;

    QObject *object = engine.rootObjects().first();
    QObject *rect = object->findChild<QObject*>("rect");
    if(object){
        QVariant sum;
        QMetaObject::invokeMethod(object, "myAdd", Q_RETURN_ARG(QVariant, sum), Q_ARG(QVariant, 1), Q_ARG(QVariant, 2));
        if(sum.isValid()){
            qDebug() << "sum : " << sum.toDouble();
        }
    }

    if(rect){
        QMetaObject::invokeMethod(rect, "myPrint");
    }

    MyTest *test = new MyTest;
    QObject *button = object->findChild<QObject*>("button");
    QObject::connect(button, SIGNAL(clicked()), test, SLOT(slotClick()));

    return app.exec();
}

**注意 ** 要注意的点就是使用的是 QObject::connect 是QT4的连接方式。注意QT4的连接方式是这么写的。

【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

  1. 分享:
最后一次编辑于 2023年11月08日 0

暂无评论

推荐阅读
  lA9hNJ8xYnRP   2023年12月12日   30   0   0 Qt
  lA9hNJ8xYnRP   2023年12月06日   31   0   0 Qt
  gBkHYLY8jvYd   2023年12月06日   50   0   0 #includecii++
  lA9hNJ8xYnRP   2023年12月06日   34   0   0 构造函数Qt
  lA9hNJ8xYnRP   2023年12月07日   30   0   0 Qt
  lA9hNJ8xYnRP   2023年12月11日   27   0   0 Qt
  gBkHYLY8jvYd   2023年12月10日   18   0   0 #include邻域灰度图像
  gBkHYLY8jvYd   2023年12月10日   22   0   0 #include数组i++
  gBkHYLY8jvYd   2023年12月06日   19   0   0 #includeios数据
  gBkHYLY8jvYd   2023年12月08日   20   0   0 #includecii++
  lA9hNJ8xYnRP   2023年11月25日   38   0   0 Qt数据
  lA9hNJ8xYnRP   2023年11月30日   30   0   0 Qt表视图
  gBkHYLY8jvYd   2023年11月22日   23   0   0 #include十进制高精度
JCsIkAITQy58