There are object cast need to be done for "root" object too
QQmlComponent component(engine, QUrl(src));
QObject *comp = component.create();
QQuickItem *item = qobject_cast<QQuickItem*>(comp);
item->setParentItem(qobject_cast<QQuickItem*>(root));
This works for me in my project:
// QtQuick Application
int main(int Counter, char *Arguments[]) {
QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
QGuiApplication Application(Counter, Arguments);
QQmlApplicationEngine Engine;
Engine.load(QUrl(Main));
if (Engine.rootObjects().isEmpty()) {
return -1;
}
QObject *oRootObject = dynamic_cast<QObject*>(Engine.rootObjects()[0]);
QObject *oBottomBlock = oRootObject->findChild<QObject*>("bottomBlock");
if (oBottomBlock) {
QQmlComponent oComponent(&Engine,QUrl(QString("qrc:/ButtonExit.qml")));
QObject *oButtonExit = oComponent.create();
QQuickItem *oItemButtonExit = qobject_cast<QQuickItem*>(oButtonExit);
oItemButtonExit->setParentItem(qobject_cast<QQuickItem*>(oBottomBlock));
}
return Application.exec();
}