Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct
Connection C++ QML [SOLVED]
Hi, could someone please recommend me the best way how to interconnect c++ with QML. I will write an application for Android which will work as TCP server. I'd like to use QML2 for GUI requirements. But I am little bit lost at this moment. The base project should be created as C++ application with connected QML files or vice versa? It would be nice if someone will have links or advice. Thank you in advance.
Follow "this":http://qt-project.org/forums/viewthread/33749/#146966 for a simple C++ - QML integration example.
"qtqml-cppintegration-topic":http://qt-project.org/doc/qt-5.0/qtqml/qtqml-cppintegration-topic.html , "qtqml-cppintegration-interactqmlfromcpp":http://qt-project.org/doc/qt-5.0/qtqml/qtqml-cppintegration-interactqmlfromcpp.html and "qtqml-cppintegration-exposecppattributes":http://qt-project.org/doc/qt-5.0/qtqml/qtqml-cppintegration-exposecppattributes.html explains all the required stuff.
I always select "Qt Quick 2 Application (Built-in Types)" from Qt Creator as base project for QML based applications as it creates all the necessary stuff.
Hope this helps..
Ok, thank you, it seems to be enough for me at this moment. Thanks again.
After reading I have still one question. What is better way.... accessing QML from C++ application or C++ parts from QML application? I'd like to make nice GUI for Android but still I need have a control on business logic in c++ too.
IMHO, i too don't know the advantages of one over the other. But i consider accessing C++ classes from QML application. It's more easier to understand. In both cases the business logic is implemented in C++ classes.
Ok, however thank you for input. Maybe somebody else will replay...
utcenter last edited by
Use signals and slots
Use Q_INVOKABLE and Q_ENUMS
This is really all you need to expose your C++ logic to QML. You can either inherit QObject if you are writing new code, or you can just wrap your existing "legacy" classes in a QObject and provide an interface with properties, signals and slots.
There was a good talk on the subject on the last devdays, while the videos are yet not available, you may still find this "PDF presentation":http://www.qtdeveloperdays.com/sites/default/files/presentation_pdf/QtDevDaysSFO-2013_WrappingCppForQml_final.pdf to be useful.
You should not access QML from C++, your C++ logic should be completely agnostic of the UI. Your C++ code should not even concern with the existence of QML, your only concern should be exposing what you need to access from C++ through the Qt meta system, which is what both C++ and QML can use directly out of the box. You can wrap individual classes and use them as building blocks or you can wrap a large scale logic, sort of a "solidified" logic core that you may instance once and use it to control your entire application. You even have the option of instantiating your wrapped objects in QML or in C++ and just make them available in the QML context.
Thank you, I understood QML exploits C++ logic and C++ doesn't rely on QML GUI. This thread could be solved for me.
utcenter last edited by
Note that it is still possible to access and control QML from C++, again through the meta system, but consider this to be "last resort" and most certainly not recommended practice.
QML will handle all data pretty well, provided of course you have registered those type to the meta system. I'd say it is even safe to use raw pointers through QML with properties and methods - something most people will probably advise against for being unsafe, but hey, pointers are unsafe in C++ too :) I actually work on a project that passes around C++ pointers between components instantiated in QML and it works flawlessly.