⚠️ Forum Maintenance: Feb 6th, 8am - 14pm (UTC+2)
Qt Quick and C - different ways of linking QML and C/C++
marcin100 last edited by
Could someone list different ways of linking a QML object's action with the C/C++ code behind. I have done some reading, most confusing to me and still cannot get a good grasp on this.
A QML defined button (via QtQuick 1.1 / Qt 4.8) is pressed on the touchscreen (or clicked with a mouse).
This action should call a C/C++ function in the .cpp file
Is there only one way of achieving this or many? (code examples would be greatly appreciated)
What are the different ways, if any?
What are the reasons for these different ways, if any?
You need to expose a QObject to QML context. Then you can run any method marked as Q_INVOKABLE and modify any Q_PROPERTYies that it has.
Another option is to inherit from QQuickItem (QDeclarativeItem in your case) and expose methods there (same story: Q_INOVKABLE, QPROPERTY).
Check out my CCF project - it's probably to complicated as an example, but there you go. qmlRegisterType lines register custom kids of QQuickItem, while lines 69-73 register standard QObjects. You can find base classes for those objects in "the repo":https://github.com/sierdzio/closecombatfree/tree/master/src.