Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling C++ method from QML (Qt Quick application)

Tags:

c++

qt

qt-quick

So, I've done some searching and none of the similar questions I've read have had advice that worked.

I'm using Qt Creator (and I'm not too familiar with Qt) so I'm not sure what voodoo it's done in the background. However, I'm using a standard Qt Quick Application project.

Essentially, I want to call a C++ function from QML that returns a string that replaces some text in the layout, periodically.

Here is main.cpp:

#include <QtGui/QApplication>
#include "qmlapplicationviewer.h"
#include <QDeclarativeContext>


class testClass : public QObject
{
    Q_OBJECT
public:
    Q_INVOKABLE QString gimmeText() {
            return QString("new text");
}
};



Q_DECL_EXPORT int main(int argc, char *argv[])
{
    QScopedPointer<QApplication> app(createApplication(argc, argv));

    QmlApplicationViewer viewer;

    viewer.setOrientation(QmlApplicationViewer::ScreenOrientationAuto);
    viewer.setMainQmlFile(QLatin1String("qml/Picenter2/main.qml"));

    testClass t;
    viewer.rootContext()->setContextProperty("testOb", &t);

    viewer.showFullScreen();

    return app->exec();
}

And here is a snippet of the layout (as most of it is obviously irrelevant):

Text {
    id: text1
    x: 105
    y: 156
    color: "#ffffff"
    text: qsTr("text")
    font.pixelSize: 12
    Timer {
        interval: 1000; running: true; repeat: false
        onTriggered: text1.text = testOb.gimmeText();
    }

The errors given are:

invalid use of incomplete type 'struct QDeclarativeContext' main.cpp (28)
forward declaration of 'struct QDeclarativeContext' qdeclarativeview.h (60)

EDIT: with QDeclarativeContext included, the above disappear, giving these errors:

(.text.startup+0x3e):-1: error: undefined reference to `vtable for testClass'
(.text.startup+0xcf):-1: error: undefined reference to `vtable for testClass'
(.text.startup+0x12b):-1: error: undefined reference to `vtable for testClass'
:-1: error: collect2: ld returned 1 exit status

I haven't done much C++ programming, so I'm not entirely familiar with what that means. Following advice for essentially identical problems has only given me vtable errors or more incomprehensible things.

What really confuses me is that, looking at the header file, the QmlApplicationViewer is derived from QDeclarativeView, which is exactly what the Qt documentation uses here to do almost exactly what I want. Thanks for any suggestions anyone has.

like image 559
Logan Avatar asked Dec 11 '12 07:12

Logan


People also ask

How do you call a QML function in C++?

All QML methods are exposed to the meta-object system and can be called from C++ using QMetaObject::invokeMethod(). You can specify types for the parameters and the return value after the colon character, as shown in the code snippet below.

What is QObject in QML?

The QtObject type is a non-visual element which contains only the objectName property.

How do you use QML in Qt?

Creating and Running QML Projects For simple UI files such as this one, select File > New File or Project > Application (Qt Quick) > Qt Quick Application - Empty from within Qt Creator. Pressing the green Run button runs the application. You should see the text Hello, World! in the center of a red rectangle.

What is Q_property in QML?

Exposing Properties. A property can be specified for any QObject-derived class using the Q_PROPERTY() macro. A property is a class data member with an associated read function and optional write function. All properties of a QObject-derived or Q_GADGET class are accessible from QML.


1 Answers

You have to register your class for using it with QML. You can do this in the main function. You have to import it in the QML code too. Your code should look like this :

main.cpp :

#include <QtGui/QApplication>
#include "qmlapplicationviewer.h"
#include <QDeclarativeContext>

#include <QtDeclarative>    // Required for registration


class testClass : public QObject
{
    Q_OBJECT
    public:
    Q_INVOKABLE QString gimmeText() {
            return QString("new text");
    }
};



Q_DECL_EXPORT int main(int argc, char *argv[])
{
    QScopedPointer<QApplication> app(createApplication(argc, argv));

    qmlRegisterType<testClass>("MyCustomQMLObjects", 2, 35, "testClassNameInQML");

    QmlApplicationViewer viewer;

    viewer.setOrientation(QmlApplicationViewer::ScreenOrientationAuto);
    viewer.setMainQmlFile(QLatin1String("qml/Picenter2/main.qml"));

    testClass t;
    viewer.rootContext()->setContextProperty("testOb", &t);

    viewer.showFullScreen();

    return app->exec();
}

QML code :

// ...

import MyCustomQMLObjects 2.35

// ...

property testClassNameInQML testOb

// ...

Text {
    id: text1
    x: 105
    y: 156
    color: "#ffffff"
    text: qsTr("text")
    font.pixelSize: 12
    Timer {
        interval: 1000; running: true; repeat: false
        onTriggered: text1.text = testOb.gimmeText();
    }

// ...
like image 77
air-dex Avatar answered Oct 16 '22 09:10

air-dex