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: <github-actions-qt-creator@cristianadam.eu>
Reviewed-by: Alessandro Portale <alessandro.portale@qt.io>
This commit is contained in:
Fabian Kosmale
2023-01-30 11:12:10 +01:00
parent 86ffc29239
commit fa2ea6fd97
5 changed files with 44 additions and 6 deletions

View File

@@ -2,10 +2,22 @@ cmake_minimum_required(VERSION 3.16)
project(%{ProjectName} VERSION 0.1 LANGUAGES CXX) project(%{ProjectName} VERSION 0.1 LANGUAGES CXX)
@if !%{HasQSPSetup}
set(CMAKE_AUTOMOC ON) set(CMAKE_AUTOMOC ON)
@endif
set(CMAKE_CXX_STANDARD_REQUIRED ON) 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} qt_add_executable(%{TargetName}
main.cpp main.cpp
@@ -14,7 +26,7 @@ qt_add_executable(%{TargetName}
qt_add_qml_module(%{TargetName} qt_add_qml_module(%{TargetName}
URI %{ProjectName} URI %{ProjectName}
VERSION 1.0 VERSION 1.0
QML_FILES main.qml QML_FILES Main.qml
) )
set_target_properties(%{TargetName} PROPERTIES set_target_properties(%{TargetName} PROPERTIES

View File

@@ -10,13 +10,25 @@ int main(int argc, char *argv[])
QGuiApplication app(argc, argv); QGuiApplication app(argc, argv);
QQmlApplicationEngine engine; 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, QObject::connect(&engine, &QQmlApplicationEngine::objectCreated,
&app, [url](QObject *obj, const QUrl &objUrl) { &app, [url](QObject *obj, const QUrl &objUrl) {
if (!obj && url == objUrl) if (!obj && url == objUrl)
QCoreApplication::exit(-1); QCoreApplication::exit(-1);
}, Qt::QueuedConnection); }, Qt::QueuedConnection);
@endif
@if %{HasLoadFromModule}
engine.loadFromModule("%{JS: value('ProjectName')}", "Main");
@else
engine.load(url); engine.load(url);
@endif
return app.exec(); return app.exec();
} }

View File

@@ -16,6 +16,6 @@ CppApplication {
Group { Group {
Qt.core.resourcePrefix: "%{ProjectName}/" Qt.core.resourcePrefix: "%{ProjectName}/"
fileTags: ["qt.qml.qml", "qt.core.resource_data"] fileTags: ["qt.qml.qml", "qt.core.resource_data"]
files: ["main.qml"] files: ["Main.qml"]
} }
} }

View File

@@ -17,6 +17,10 @@
{ "key": "MainCppFileName", "value": "%{JS: 'main.' + Util.preferredSuffix('text/x-c++src') }" }, { "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": "UseVirtualKeyboardByDefault", "value": "%{JS: value('Plugins').indexOf('Boot2Qt') >= 0 || value('Plugins').indexOf('Boot2QtQdb') >= 0 }" },
{ "key": "TargetName", "value": "%{JS: 'app' + value('ProjectName') }" }, { "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": "QdsWizardPath", "value": "%{IDE:ResourcePath}/qmldesigner/studio_templates/projects" },
{ "key": "QdsProjectStyle", "value": "%{JS: value('BuildSystem') === 'cmake' ? %{QdsProjectStyleInput} : false }" }, { "key": "QdsProjectStyle", "value": "%{JS: value('BuildSystem') === 'cmake' ? %{QdsProjectStyleInput} : false }" },
{ "key": "NoQdsProjectStyle", "value": "%{JS: !%{QdsProjectStyle} }" }, { "key": "NoQdsProjectStyle", "value": "%{JS: !%{QdsProjectStyle} }" },
@@ -104,6 +108,16 @@
{ {
"checked": "%{UseVirtualKeyboardByDefault}" "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}" "condition": "%{NoQdsProjectStyle}"
}, },
{ {
"source": "main.qml.tpl", "source": "Main.qml.tpl",
"target": "main.qml", "target": "Main.qml",
"openInEditor": true, "openInEditor": true,
"condition": "%{NoQdsProjectStyle}" "condition": "%{NoQdsProjectStyle}"
}, },