QQMLComponent not showing up in main Window
-
Hi there, I am following the documentation to call a QML component from c++ and display it in the main window: http://doc.qt.io/qt-5/qtqml-cppintegration-interactqmlfromcpp.html
In main.cpp i have the following:
QQmlApplicationEngine engine; engine.load(QUrl(QStringLiteral("qrc:/main.qml"))); QQmlComponent component(&engine, QUrl::fromLocalFile(":/Circle2.qml")); QObject *object = component.create();
The path ":/Circle2.qml" as opposed to "Circle2.qml" or "qrc:/Circle2.qml" is the only path that does not generate the error "QQmlComponent: Component is not ready" as described in this post: https://forum.qt.io/topic/54517/solved-qqmlcomponent-component-is-not-ready/9
HOWEVER, even without the error, the Circle component does not show up in my main window.
my main.qml:
import QtQuick 2.9 import QtQuick.Window 2.2 Window { visible: true width: 640 height: 300 title: qsTr("Hello World") color: "green" /*Circle{ //width: parent.width/16 //height: parent.height/8 anchors.centerIn: parent }*/ }
and my Circle2.qml file:
import QtQuick 2.4 Rectangle{ width: 50 height: 50 color: "darkblue" //border.color: "blue" border.width: 0 radius: width*0.5 }
Does anyone know why my circle isnt displaying anywhere?
-
Hi, did you get the answer for it. Did you resolve it?
-
The C++ code snippet in OP is missing a call to set the parent item of the instantiated component. Without that, it's not part of a scene graph.
From https://doc.qt.io/qt-6/qqmlcomponent.html
QQmlComponent component(engine, QUrl::fromLocalFile("MyItem.qml")); QQuickItem *childItem = qobject_cast<QQuickItem*>(component.create()); childItem->setParentItem(this);