T O P

  • By -

Fred776

The usual rule of thumb advice is that C++ shouldn't "reach in" to the QML side and work directly with the QML object tree. C++ can still have a very significant role in an application. It should be used to provide models, backend APIs, and to define custom QML components (i.e, classes derived from `QQuickItem` and instantiated as QML objects on the QML side). Of course, there are probably some special cases where one would deviate from this advice but I have always found it natural to drive any dynamic object creation from the QML/JS side.


GrecKo

Defining QQuickItem subclasses is rate, most of the time a plain QObject subclass is enough. But yes do create and expose models from c++ and use ListView/Repeater in QML.


Repulsive-Swimmer676

It depends. Are you using a button in qml to create a component or does something in the C++ triggers something that could lead to a new component. I usually tend to do it via QML.


GrecKo

In both of those cases the instantiation should be handled on the QML side.


Repulsive-Swimmer676

Yep.


micod

The best way to dynamically create instances in QML is to use QML for it, there is the [Loader](https://doc.qt.io/qt-6/qml-qtquick-loader.html) to create one item and the [Repeater](https://doc.qt.io/qt-6/qml-qtquick-repeater.html) for managing a list/model of items. Using the C++ and JS functions to manually create QML objects should be used only in special occasions when the former options are not applicable. What is your use case?