I need to add QML components at runtime from C++ code. I am able to create ApplicationWindow
from 'main.qml' file. The window is displayed successfully. The issue is that I am not able to add other QML components to this window. I have button specified in 'button.qml' file. So I tried to create another QQmlComponent
and set the ApplicationWindow
as parent to the button. The output of obj1->children()
shows that the children of type button exists
(QQuickItem(0xcc08c0) , Button_QMLTYPE_12(0xa7e6d0) ). But the button is not displayed.
When I try to add Button staticaly into 'main.qml' everything works well. I am missing something in creation of QQmlComponent
at runtime.
QQmlEngine engine;
QQmlComponent component1(&engine, QUrl("qrc:/main.qml"));
QQmlComponent component2(&engine, QUrl("qrc:/button.qml"));
QObject* obj1 = component1.create();
QObject* obj2 = component2.create();
obj2->setParent(obj1);