I want to instantiate a simple qml file with a rectangle through c++ . Unfortunately nothing shows up. I have no idea what the problem could be, the out-commented error printing shows no errors.
This is the Source Code:
#include <QGuiApplication>#include <QQmlApplicationEngine>#include <QQmlComponent>#include <QFile>#include <QDebug>int main(int argc, char *argv[]){#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0) QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);#endif QGuiApplication app(argc, argv); QQmlApplicationEngine engine; const QUrl url(QStringLiteral("qrc:/main.qml")); QObject::connect(&engine, &QQmlApplicationEngine::objectCreated,&app, [url](QObject *obj, const QUrl &objUrl) { if (!obj && url == objUrl) QCoreApplication::exit(-1); }, Qt::QueuedConnection); engine.load(url); QQmlComponent component(&engine, QUrl::fromLocalFile("C:/Workspace/QT/QMLPlayArea/InstantiableObject.qml"), engine.rootObjects().first());// if (component.status() != QQmlComponent::Ready) {// if (component.status() == QQmlComponent::Error)// qDebug() << "Error loading component:" << component.errorString();// return -1;// } QObject *object = component.create(); object->setProperty("width", 500); object->setProperty("height", 500); object->setProperty("color", "red"); return app.exec();}
The QML main.qml is the default when creating a new project. the InstantiatableObject.qml is :
import QtQuick 2.14Item { Rectangle{ width: 100 height: 100 color: "red" }}