Qt5 QML/C++ integration
spompelio last edited by
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?
mbrasser last edited by
[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).
chriadam last edited by
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 ;-)
jech last edited by
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:
class MyClass : public QObject
explicit MyClass(QObject *parent = 0);
#endif // MYCLASS_H
MyClass::MyClass(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!
jech last edited by
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?
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.