Qt5 QML/C++ integration



  • I'm really impressed with the capabilities in Qt5's QtQuick demos. I have an existing qt4 application that I'd like to add some of the Qt5 visual effects in. I'm thinking I should completely replace the UI portions with some sort of QML/c++ integration but I'm not sure where to start. The documentaton and demos for Qt5 seem to only show applications that use QML and javascript and just a little c++ to load the scene.

    My application is very dynamic and loads widgets at runtime based on data coming off the network so laying out a static UI in QML won't work. I'd like to keep the existing C++ code that creates and modifies the widgets so recreating everything in javascript doesn't seem worth the effort. If I were to use Qt4 my assumtption would be to create widgets on the fly with QDeclarativeComponent but that's changed in Qt5.

    Anyone have thoughts or could point to any existing demo that uses C++ to do most of the legwork but uses QML to display the widgets?



  • [quote author="spompelio" date="1345410534"]If I were to use Qt4 my assumtption would be to create widgets on the fly with QDeclarativeComponent but that's changed in Qt5.[/quote]

    A number of classes were renamed for QtQuick 2. In this case, QDeclarativeComponent was renamed to QQmlComponent (and it should essentially be a drop-in replacement for QDeclarativeComponent).

    Regards,
    Michael



  • The QML/C++ integration documentation has been updated for Qt 5. See "cpp integration docs":http://doc-snapshot.qt-project.org/5.0/qtqml-cppintegration-topic.html for more information.
    If there's anything you think should be clarified in those docs, please let us know.

    As for demos which are mainly C++ with a QML front-end, we don't have many of those, but if you know how to integrate C++ data and controllers into QML-defined scenes, it's fairly straightforward. Q_PROPERTY and Q_INVOKABLE are your friends ;-)

    Cheers,
    Chris.



  • I know this is a pretty old thread, but my question fits into this topic.

    Because I would like to port my application to Android and there is (and might not ever be) PySide with Qt5 support, I'm converting my Python/QML application to C++/QML. The problem is that I have almost no experience with C++. I tried to search the web for some guides which would help me but I couldn't find anything that would help me much.

    I need to call a function from QML with a parameter. The function should return a list back. From some code samples and guides I understood the cleanest way to do this would be to create a new class. I used QtCreator to do it and this is what I got:

    myclass.h
    @#ifndef MYCLASS_H
    #define MYCLASS_H

    #include <QObject>

    class MyClass : public QObject
    {
    Q_OBJECT
    public:
    explicit MyClass(QObject *parent = 0);

    signals:

    public slots:

    };

    #endif // MYCLASS_H
    @

    myclass.cpp
    @#include "myclass.h"

    MyClass::MyClass(QObject *parent) :
    QObject(parent)

    {
    }
    @

    I know how to expose the class to QML. But I need to define the function, that I want to call from QML. I suppose in myclass.h should be just the header of the function and the body should be in the .cpp file. Could you write me a short example how it should look like, please?

    Another question is, whether the function will run in a separate thread or it will block the main QML UI. I'm using threads for this in my current Python code, so I suppose I'll have to do the same here. But I read somewhere that QtQuick 2.0 runs the UI in a separate thread. So I'll rather ask...

    Thanks a lot in advance!



  • After many experiments and studying of code samples I got it to work. But as I was afraid, the function blocks the main UI. So I need to run it in a separate thread. So again I'll have to read many documentation and study how to make it... :-(

    Reading through the Qt documentation and code samples. I found out that I can do the same thing using either a slot or a function (method). What is the difference between these 2 options?


  • Moderators

    Qt documentation has a great entry on signals and slots, just read it ;)

    In general, a slot is just a standard C++ method, but it has some additional features: it can be invoked through meta object system (QObject::incokeMethod()), signals can be connected to it, and it can use the QObject::sender() meta information (although it's pretty dangerous). Another benefit is that it can be invoked indirectly (Qt::QueuedConnection), which helps greatly in making asynchronous APIs, fluid GUIs and safe communication with threads.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.