From fa2ea6fd97c9e34b317f71550388c3f97aa18601 Mon Sep 17 00:00:00 2001 From: Fabian Kosmale Date: Mon, 30 Jan 2023 11:12:10 +0100 Subject: [PATCH] QtQuickApplication template: Support more modern API (Re)add a way to select the minimum supported Qt version. If it is >= 6.4, connect to QQmlApplicationEngine::objectCreationFailed instead of objectCreated, getting rid of the status check. Moreover, if we have Qt >= 6.4, also use qt_standard_project_setup instead of manually enabling AUTOMOC. That is available since 6.3, but no need to clutter the version selection list with too many entries. If only 6.5 or higher is required, put the QML files under AUTO_RESOURCE_PREFIX, and use loadFromModule instead of load. Unconditionally rename main.qml to Main.qml, which is necessary for loadFromModule to work, and making this conditional on the used Qt version is more trouble than it is worth. For qbs, we do not yet put the files under the auto resource prefix, so do not use loadFromModule there. Fixes: QTBUG-47996 Change-Id: Ib8ca375c2316b58eecf54009d36fc9f7dac7c1f4 Reviewed-by: Reviewed-by: Alessandro Portale --- .../projects/qtquickapplication/CMakeLists.txt | 16 ++++++++++++++-- .../{main.qml.tpl => Main.qml.tpl} | 0 .../projects/qtquickapplication/main.cpp | 14 +++++++++++++- .../projects/qtquickapplication/tmpl.qbs | 2 +- .../projects/qtquickapplication/wizard.json | 18 ++++++++++++++++-- 5 files changed, 44 insertions(+), 6 deletions(-) rename share/qtcreator/templates/wizards/projects/qtquickapplication/{main.qml.tpl => Main.qml.tpl} (100%) diff --git a/share/qtcreator/templates/wizards/projects/qtquickapplication/CMakeLists.txt b/share/qtcreator/templates/wizards/projects/qtquickapplication/CMakeLists.txt index d5725099e15..63e2498dff2 100644 --- a/share/qtcreator/templates/wizards/projects/qtquickapplication/CMakeLists.txt +++ b/share/qtcreator/templates/wizards/projects/qtquickapplication/CMakeLists.txt @@ -2,10 +2,22 @@ cmake_minimum_required(VERSION 3.16) project(%{ProjectName} VERSION 0.1 LANGUAGES CXX) +@if !%{HasQSPSetup} set(CMAKE_AUTOMOC ON) +@endif set(CMAKE_CXX_STANDARD_REQUIRED ON) -find_package(Qt6 6.2 REQUIRED COMPONENTS Quick) +find_package(Qt6 %{MinimumSupportedQtVersion} REQUIRED COMPONENTS Quick) + +@if %{HasQSPSetup} +@if %{UsesAutoResourcePrefix} +qt_standard_project_setup( + MIN_VERSION 6.5 +) +@else +qt_standard_project_setup() +@endif +@endif qt_add_executable(%{TargetName} main.cpp @@ -14,7 +26,7 @@ qt_add_executable(%{TargetName} qt_add_qml_module(%{TargetName} URI %{ProjectName} VERSION 1.0 - QML_FILES main.qml + QML_FILES Main.qml ) set_target_properties(%{TargetName} PROPERTIES diff --git a/share/qtcreator/templates/wizards/projects/qtquickapplication/main.qml.tpl b/share/qtcreator/templates/wizards/projects/qtquickapplication/Main.qml.tpl similarity index 100% rename from share/qtcreator/templates/wizards/projects/qtquickapplication/main.qml.tpl rename to share/qtcreator/templates/wizards/projects/qtquickapplication/Main.qml.tpl diff --git a/share/qtcreator/templates/wizards/projects/qtquickapplication/main.cpp b/share/qtcreator/templates/wizards/projects/qtquickapplication/main.cpp index bdfd80e1c35..155667fb122 100644 --- a/share/qtcreator/templates/wizards/projects/qtquickapplication/main.cpp +++ b/share/qtcreator/templates/wizards/projects/qtquickapplication/main.cpp @@ -10,13 +10,25 @@ int main(int argc, char *argv[]) QGuiApplication app(argc, argv); QQmlApplicationEngine engine; - const QUrl url(u"qrc:/%{JS: value('ProjectName')}/main.qml"_qs); +@if !%{HasLoadFromModule} + const QUrl url(u"qrc:/%{JS: value('ProjectName')}/Main.qml"_qs); +@endif +@if %{HasFailureSignal} + QObject::connect(&engine, &QQmlApplicationEngine::objectCreationFailed, + &app, []() { QCoreApplication::exit(-1); }, + Qt::QueuedConnection); +@else QObject::connect(&engine, &QQmlApplicationEngine::objectCreated, &app, [url](QObject *obj, const QUrl &objUrl) { if (!obj && url == objUrl) QCoreApplication::exit(-1); }, Qt::QueuedConnection); +@endif +@if %{HasLoadFromModule} + engine.loadFromModule("%{JS: value('ProjectName')}", "Main"); +@else engine.load(url); +@endif return app.exec(); } diff --git a/share/qtcreator/templates/wizards/projects/qtquickapplication/tmpl.qbs b/share/qtcreator/templates/wizards/projects/qtquickapplication/tmpl.qbs index 3c5b8127cc9..050ace84150 100644 --- a/share/qtcreator/templates/wizards/projects/qtquickapplication/tmpl.qbs +++ b/share/qtcreator/templates/wizards/projects/qtquickapplication/tmpl.qbs @@ -16,6 +16,6 @@ CppApplication { Group { Qt.core.resourcePrefix: "%{ProjectName}/" fileTags: ["qt.qml.qml", "qt.core.resource_data"] - files: ["main.qml"] + files: ["Main.qml"] } } diff --git a/share/qtcreator/templates/wizards/projects/qtquickapplication/wizard.json b/share/qtcreator/templates/wizards/projects/qtquickapplication/wizard.json index 885a8487e31..88d63e19f9a 100644 --- a/share/qtcreator/templates/wizards/projects/qtquickapplication/wizard.json +++ b/share/qtcreator/templates/wizards/projects/qtquickapplication/wizard.json @@ -17,6 +17,10 @@ { "key": "MainCppFileName", "value": "%{JS: 'main.' + Util.preferredSuffix('text/x-c++src') }" }, { "key": "UseVirtualKeyboardByDefault", "value": "%{JS: value('Plugins').indexOf('Boot2Qt') >= 0 || value('Plugins').indexOf('Boot2QtQdb') >= 0 }" }, { "key": "TargetName", "value": "%{JS: 'app' + value('ProjectName') }" }, + { "key": "HasQSPSetup", "value": "%{JS: value('MinimumSupportedQtVersion') > '6.2' }"}, + { "key": "HasFailureSignal", "value": "%{JS: value('MinimumSupportedQtVersion') > '6.3' }"}, + { "key": "UsesAutoResourcePrefix", "value": "%{JS: value('MinimumSupportedQtVersion') > '6.4' && value('BuildSystem') === 'cmake' }"}, + { "key": "HasLoadFromModule", "value": "%{JS: value('MinimumSupportedQtVersion') > '6.4' && value('UsesAutoResourcePrefix') }"}, { "key": "QdsWizardPath", "value": "%{IDE:ResourcePath}/qmldesigner/studio_templates/projects" }, { "key": "QdsProjectStyle", "value": "%{JS: value('BuildSystem') === 'cmake' ? %{QdsProjectStyleInput} : false }" }, { "key": "NoQdsProjectStyle", "value": "%{JS: !%{QdsProjectStyle} }" }, @@ -104,6 +108,16 @@ { "checked": "%{UseVirtualKeyboardByDefault}" } + }, + { + "name": "MinimumSupportedQtVersion", + "trDisplayName": "The minimum version of Qt you want to build the application for", + "type": "ComboBox", + "data": + { + "items": [ "6.2", "6.4", "6.5" ], + "index": 1 + } } ] }, @@ -146,8 +160,8 @@ "condition": "%{NoQdsProjectStyle}" }, { - "source": "main.qml.tpl", - "target": "main.qml", + "source": "Main.qml.tpl", + "target": "Main.qml", "openInEditor": true, "condition": "%{NoQdsProjectStyle}" },