Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Language Bindings
  4. QML/C++ bindings
Forum Updated to NodeBB v4.3 + New Features

QML/C++ bindings

Scheduled Pinned Locked Moved Language Bindings
5 Posts 2 Posters 7.1k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • W Offline
    W Offline
    wickwire
    wrote on last edited by
    #1

    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:

    doc.qt.digia.com/qt/qtbinding.html -> which seems to be down at the moment
    same info here -> http://harmattan-dev.nokia.com/docs/library/html/qt4/qtbinding.html#exchanging-data-between-qml-and-c

    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!

    1 Reply Last reply
    0
    • sierdzioS Online
      sierdzioS Online
      sierdzio
      Moderators
      wrote on last edited by
      #2

      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":https://github.com/sierdzio/closecombatfree/blob/master/src/ccfmain.cpp (based on Qt5 and QML2).

      (Z(:^

      1 Reply Last reply
      0
      • W Offline
        W Offline
        wickwire
        wrote on last edited by
        #3

        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:

        http://doc.qt.digia.com/qt/qtbinding.html

        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")
                myObject.cppSlot(12345)
            }
        }
        

        }
        @

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

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

        class MyClass : public QObject
        {
        Q_OBJECT
        public:
        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);
        
        viewer.setMainQmlFile(QStringLiteral("qml/bindings/main.qml"&#41;&#41;;
        viewer.showExpanded(&#41;;
        
        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!

        1 Reply Last reply
        0
        • sierdzioS Online
          sierdzioS Online
          sierdzio
          Moderators
          wrote on last edited by
          #4

          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.

          (Z(:^

          1 Reply Last reply
          0
          • W Offline
            W Offline
            wickwire
            wrote on last edited by
            #5

            thanks, that was it!

            1 Reply Last reply
            0

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved