QML instantiates C++ objects. How do I access their methods?

时间:2019-02-18 00:48:00

标签: c++ qt qt5 qtquick2

Here's what my main.cpp looks like:

int main(int argc, char **argv)
{
    QGuiApplication app(argc, argv);
    QCoreApplication::addLibraryPath("./");

    QQuickView view;
    view.setResizeMode(QQuickView::SizeRootObjectToView);
    view.setSource(QUrl("qrc:/myqml.qml"));
    view.show();

    return app.exec();
}

As you can see, it creates things from myqml. Well, myqml instantiates a C++ class MyClass.

How do I access this C++ methods from the object QQuickView view? For example, I'd like to do something of the type view.MyClassInstance.myMethod1()

1 个答案:

答案 0 :(得分:2)

您要获取由C ++在QML中创建的对象,在这里,目标是否具有在C ++中创建的原型都没有关系。如果需要,您必须通过findChild获得对象,因为在QML中创建的所有对象都与窗口有亲属关系。

main.cpp

#include <QtQuick>
#include <QtGui>

class MyClass: public QObject
{
    Q_OBJECT
public:
    using QObject::QObject;
    Q_INVOKABLE void invokable(){
        qDebug()<< "invokable";
    }
    Q_SLOT void slot(){
        qDebug()<< "slot";
    }
    void function(){
        qDebug()<< "function";
    }
};

int main(int argc, char *argv[])
{
    QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
    qmlRegisterType<MyClass>("foo", 1, 0, "MyClass");
    QGuiApplication app(argc, argv);

    QQuickView view;
    view.setResizeMode(QQuickView::SizeRootObjectToView);
    view.setSource(QUrl(QStringLiteral("qrc:/main.qml")));
    view.show();
    if(MyClass* myclass_instance = view.findChild<MyClass *>("myclass_instance")){
        myclass_instance->invokable();
        myclass_instance->slot();
        myclass_instance->function();
    }
    return app.exec();
}
#include "main.moc"

main.qml

import QtQuick 2.9
import foo 1.0

Rectangle {
    color: "salmon"
    width: 640
    height: 480
    MyClass{
        objectName: "myclass_instance"
    }
}

但是此方法有几个缺点,例如谁管理对象的生命周期是QML,而不是C ++,因此指针可能在某个时候指向未保留的地址。另一个缺点是C ++对QML有依赖性,因为如果在QML中更改了ObjectName,则必须在C ++中更改代码。


另一种方法是创建一个使用setContextProperty()导出到QML并与MyClass对象进行交互的助手类。

main.cpp

#include <QtQuick>
#include <QtGui>

class MyClass: public QObject
{
    Q_OBJECT
public:
    using QObject::QObject;
    Q_INVOKABLE void invokable(){
        qDebug()<< "invokable";
    }
    Q_SLOT void slot(){
        qDebug()<< "slot";
    }
    void function(){
        qDebug()<< "function";
    }
};

class Helper: public QObject
{
    Q_OBJECT
public:
    using QObject::QObject;
    void call_function(){
        emit called();
    }
    Q_SIGNAL void called();
};

int main(int argc, char *argv[])
{
    QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
    qmlRegisterType<MyClass>("foo", 1, 0, "MyClass");
    QGuiApplication app(argc, argv);

    Helper helper;

    QQuickView view;
    view.rootContext()->setContextProperty("helper", &helper);
    view.setResizeMode(QQuickView::SizeRootObjectToView);
    view.setSource(QUrl(QStringLiteral("qrc:/main.qml")));
    view.show();

    helper.call_function();
    return app.exec();
}
#include "main.moc"

main.qml

import QtQuick 2.9
import foo 1.0

Rectangle {
    color: "salmon"
    width: 640
    height: 480
    MyClass{
        id: myclass
    }
    Connections{
        target: helper
        onCalled:{
            myclass.invokable()
            myclass.slot()
        }
    }
}

此方法的优点是C ++和QML的逻辑之间没有依赖关系,而且生命周期不会产生问题,因为myclass对象不是直接在QML中处理的。缺点是您要编写更多代码,并且只能调用Q_INVOKABLE或Q_SLOT。