From dcb8681cb79cb91df14bc051025a1bf9705f7c84 Mon Sep 17 00:00:00 2001 From: Ulf Hermann Date: Fri, 29 Sep 2017 15:56:36 +0200 Subject: [PATCH] Wizards: Accept asynchronous root components for qtquickapplication There are a number of reasons why the root component of a QtQuick application may be loaded asynchronously. It might, for example, be derived from a component loaded over the network. In that case, checking that there is a root object right after calling load() is misleading. The object is only created later. Therefore, we need to listen for the objectCreated signal to decide if the root component was correctly created. As calling qApp->exit(-1) does nothing while the event loop isn't running, we always connect the check with Qt::QueuedConnection. Change-Id: Ie2814894b79bb3e467c5a838c7ec419291fcf591 Reviewed-by: Tim Jenssen Reviewed-by: Leena Miettinen --- .../projects/qtquickapplication/main.cpp | 22 ++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) 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(); }