Important: Please read the Qt Code of Conduct -

QML/C++ bindings

  • Hi,

    I've been trying to exchange data between C++ and qml on Qt5, without success thus far. I've searched the documentation and found a guide on this, but for Qt 4.7/4.8 - and applying the simple example resulted in several errors: -> which seems to be down at the moment
    same info here ->

    If possible, I would like to use C++ to obtain several system information such as network interface information, cpu/memory load, etc - and then pass that on to the QML files and display it on the QML/QtQuick 2 App I have.

    I also tried QtMobility but that seems more "mobile device oriented" - I was hoping to achieve general results with C++.

    Are there any updated examples for Qt5? Should there be, or did things change from Qt4 to Qt5?

    Thanks in advance!

  • Moderators

    This works in Qt5, too, you just need to update class names and includes (QDecalarativeView -> QQuickView, QApplication -> QGuiApplication, etc. At least when you want to upgrade to QML2 awesomeness).

    If you want specific help, though, show us the errors you are getting. If you want a working example, you can take a look at my "CCF": (based on Qt5 and QML2).

  • Hi, thanks for your support!

    I'm not very C++ savvy, and I'm just learning QML as well, so I started with the example on the C++/QML bindings:

    Qt Creator > New Project... > Qt Quick 2 Application (Built-in Elements)

    And then I edited the mail.qml file to look like this:

    // MyItem.qml
    import QtQuick 2.0

    Item {
    width: 100; height: 100

    MouseArea {
        anchors.fill: parent
        onClicked: {
            myObject.cppMethod("Hello from QML")


    and then I edited the main.cpp to look like this:

    #include <QtGui/QGuiApplication>
    #include "qtquick2applicationviewer.h"
    #include <QtDebug>
    #include <QQmlContext>

    class MyClass : public QObject
    Q_INVOKABLE void cppMethod(const QString &msg) {
    qDebug() << "Called the C++ method with" << msg;

    public slots:
    void cppSlot(int number) {
    qDebug() << "Called the C++ slot with" << number;

    int main(int argc, char *argv[])
    QGuiApplication app(argc, argv);

    QtQuick2ApplicationViewer viewer;
    MyClass myClass;
    viewer.rootContext()->setContextProperty("myObject", &myClass);
    return app.exec(&#41;;


    Before this, I had several errors with debug not working, QDeclarative not available, etc - right now, not entirely sure if I'm going the right way with this, when building I get:

    main.cpp:7: undefined reference to `vtable for MyClass'

    As I understand it, once this example runs, the main.qml will be passing parameters to the main.cpp which will in turn pass them to debug.

    My final objective would be to use main.cpp to obtain the computer's MAC address and use QML to fetch that information from C++ and display it.

    Basically, given this scenario, I'd just like to know if I'm going the right way with this between C++ and QML.

    I managed to build your game as well, I've been trying to understand the code as well, hopefully helping me in my quest!

    Also and more importantly, are there any online courses planned for Qt5/QML/QtQuick? I would be very interested in that as well.

    Thank you once more for your help!

  • Moderators

    You need to put MyClass in separate files (.cpp and .h). MOC parses all classes that are added to .pro file, but omits main.cpp.

    Also, please surround your code (here in forum posts) between '@' tags.

  • thanks, that was it!

Log in to reply