Creating QML class from C++
-
HI everyone.
I would like to know if it was possible to create a QML class during the execution from C++ code or from QML with signals and slots ?
Thanks in advance. Antoine.
-
What do you mean from QML with signals and slots?
You absolutely can invoke a new instance of a Cpp class from QML depending on how you register/expose it. There's a flowchart here to help @ https://doc.qt.io/qt-5/qtqml-cppintegration-overview.html#choosing-the-correct-integration-method-between-c-and-qml
You could create a QML object from Cpp but... why would you? That just sounds like you're doing yourself a hurt.
On the same page, there's warning / advice:
https://doc.qt.io/qt-5/qtqml-cppintegration-overview.html#interacting-with-qml-objects-from-cWarning: Although it is possible to access QML objects from C++ and manipulate them, it is not the recommended approach, except for testing and prototyping purposes. One of the strengths of QML and C++ integration is the ability to implement UIs in QML separate from the C++ logic and dataset backend, and this fails if the C++ side starts manipulating QML directly. Such an approach also makes changing the QML UI difficult without affecting its C++ counterpart.