diff --git a/share/qtcreator/templates/wizards/projects/qtquickapplication/main.cpp b/share/qtcreator/templates/wizards/projects/qtquickapplication/main.cpp index 3b6ab97c69d..1cf20043ce8 100644 --- a/share/qtcreator/templates/wizards/projects/qtquickapplication/main.cpp +++ b/share/qtcreator/templates/wizards/projects/qtquickapplication/main.cpp @@ -16,9 +16,25 @@ int main(int argc, char *argv[]) QGuiApplication app(argc, argv); QQmlApplicationEngine engine; - engine.load(QUrl(QStringLiteral("qrc:/main.qml"))); - if (engine.rootObjects().isEmpty()) - return -1; + + const QUrl mainQml(QStringLiteral("qrc:/main.qml")); + + // Catch the objectCreated signal, so that we can determine if the root component was loaded + // successfully. If not, then the object created from it will be null. The root component may + // get loaded asynchronously. + const QMetaObject::Connection connection = QObject::connect( + &engine, &QQmlApplicationEngine::objectCreated, + &app, [&](QObject *object, const QUrl &url) { + if (url != mainQml) + return; + + if (!object) + app.exit(-1); + else + QObject::disconnect(connection); + }, Qt::QueuedConnection); + + engine.load(mainQml); return app.exec(); }