qt_add_qml_module: Make usage of AUTO_RESOURCE_PREFIX a policy

Writing AUTO_RESOURCE_PREFIX in every qt_add_qml_module call seems
rather pointless.

In addition:
- Add documentation for QTP0001.
- Adjust some of the examples to use QTP0001 policy.
- Improved the error message.

Task-number: QTBUG-96233
Change-Id: I6e19a491acba97493893bf1953fca3462296c1ea
Reviewed-by: Alexandru Croitor <alexandru.croitor@qt.io>
Reviewed-by: Leena Miettinen <riitta-leena.miettinen@qt.io>
(cherry picked from commit ec452fc6e8)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
This commit is contained in:
Fabian Kosmale 2022-12-08 18:28:40 +01:00 committed by Qt Cherry-pick Bot
parent 67322c08fd
commit 255ee6c962
27 changed files with 72 additions and 20 deletions

View File

@ -1,6 +1,7 @@
# Copyright (C) 2022 The Qt Company Ltd.
# SPDX-License-Identifier: BSD-3-Clause
qt6_policy(SET QTP0001 NEW)
qt6_add_qml_module(chartsplugin
URI "Charts"
PLUGIN_TARGET chartsplugin

View File

@ -4,6 +4,7 @@ endif()
set(INSTALL_EXAMPLEDIR "${INSTALL_EXAMPLESDIR}/quickcontrols/attachedstyleproperties")
qt_policy(SET QTP0001 NEW)
qt_add_qml_module(MyStyle
URI MyStyle
IMPORTS

View File

@ -25,6 +25,7 @@ target_link_libraries(chattutorial-chapter1 PRIVATE
Qt::Quick
)
qt_policy(SET QTP0001 NEW)
qt_add_qml_module(chattutorial-chapter1
URI chapter1
QML_FILES

View File

@ -6,7 +6,7 @@ CONFIG += c++11
SOURCES += main.cpp
resources.files = main.qml
resources.prefix = chapter1/
resources.prefix = qt/qml/chapter1/
RESOURCES += resources \
qtquickcontrols2.conf

View File

@ -9,7 +9,7 @@ int main(int argc, char *argv[])
QGuiApplication app(argc, argv);
QQmlApplicationEngine engine;
engine.load(QUrl(QStringLiteral("qrc:/chapter1/main.qml")));
engine.load(QUrl(QStringLiteral("qrc:/qt/qml/chapter1/main.qml")));
return app.exec();
}

View File

@ -25,6 +25,7 @@ target_link_libraries(chattutorial-chapter2 PRIVATE
Qt::Quick
)
qt_policy(SET QTP0001 NEW)
qt_add_qml_module(chattutorial-chapter2
URI chapter2
QML_FILES

View File

@ -19,7 +19,7 @@ resources.files = \
images/Hans_Gude@3x.png \
images/Hans_Gude@4x.png \
main.qml
resources.prefix = chapter2/
resources.prefix = qt/qml/chapter2/
RESOURCES += resources \
qtquickcontrols2.conf

View File

@ -9,7 +9,7 @@ int main(int argc, char *argv[])
QGuiApplication app(argc, argv);
QQmlApplicationEngine engine;
engine.load(QUrl(QStringLiteral("qrc:/chapter2/main.qml")));
engine.load(QUrl(QStringLiteral("qrc:/qt/qml/chapter2/main.qml")));
return app.exec();
}

View File

@ -25,7 +25,7 @@ target_link_libraries(chattutorial-chapter3 PRIVATE
Qt::Quick
)
# Resources:
qt_policy(SET QTP0001 NEW)
qt_add_qml_module(chattutorial-chapter3
URI chapter3
QML_FILES

View File

@ -21,7 +21,7 @@ resources.files = \
images/Hans_Gude@3x.png \
images/Hans_Gude@4x.png \
main.qml
resources.prefix = chapter3/
resources.prefix = qt/qml/chapter3/
RESOURCES += resources \
qtquickcontrols2.conf

View File

@ -9,7 +9,7 @@ int main(int argc, char *argv[])
QGuiApplication app(argc, argv);
QQmlApplicationEngine engine;
engine.load(QUrl(QStringLiteral("qrc:/chapter3/main.qml")));
engine.load(QUrl(QStringLiteral("qrc:/qt/qml/chapter3/main.qml")));
return app.exec();
}

View File

@ -28,7 +28,7 @@ target_link_libraries(chattutorial-chapter4 PRIVATE
Qt::Sql
)
# Resources:
qt_policy(SET QTP0001 NEW)
qt_add_qml_module(chattutorial-chapter4
URI chapter4
QML_FILES

View File

@ -26,7 +26,7 @@ resources.files = \
images/Hans_Gude@3x.png \
images/Hans_Gude@4x.png \
main.qml
resources.prefix = chapter4/
resources.prefix = qt/qml/chapter4/
RESOURCES += resources \
qtquickcontrols2.conf

View File

@ -43,7 +43,7 @@ int main(int argc, char *argv[])
connectToDatabase();
QQmlApplicationEngine engine;
engine.load(QUrl(QStringLiteral("qrc:/chapter4/main.qml")));
engine.load(QUrl(QStringLiteral("qrc:/qt/qml/chapter4/main.qml")));
if (engine.rootObjects().isEmpty())
return -1;

View File

@ -28,7 +28,7 @@ target_link_libraries(chattutorial-chapter5 PRIVATE
Qt::Sql
)
# Resources:
qt_policy(SET QTP0001 NEW)
qt_add_qml_module(chattutorial-chapter5
URI chapter5
QML_FILES

View File

@ -28,7 +28,7 @@ resources.files = \
images/Hans_Gude@3x.png \
images/Hans_Gude@4x.png \
main.qml
resources.prefix = chapter5/
resources.prefix = qt/qml/chapter5/
RESOURCES += resources \
qtquickcontrols2.conf

View File

@ -43,7 +43,7 @@ int main(int argc, char *argv[])
connectToDatabase();
QQmlApplicationEngine engine;
engine.load(QUrl(QStringLiteral("qrc:/chapter5/main.qml")));
engine.load(QUrl(QStringLiteral("qrc:/qt/qml/chapter5/main.qml")));
if (engine.rootObjects().isEmpty())
return -1;

View File

@ -420,6 +420,8 @@ qt_internal_add_qml_module(Qml
${extra_cmake_includes}
GENERATE_CPP_EXPORTS
GENERATE_PRIVATE_CPP_EXPORTS
POLICIES
QTP0001
)
qt_update_ignore_pch_source(Qml "compat/removed_api.cpp")

View File

@ -437,13 +437,16 @@ function(qt6_add_qml_module target)
# In that case, we assume the user knows what they want.
set(arg_RESOURCE_PREFIX "/")
else()
message(WARNING
"Neither RESOURCE_PREFIX nor AUTO_RESOURCE_PREFIX are specified for ${target}. "
"The resource root directory, ':/', is used as prefix. If this is what you want, "
"specify '/' as RESOURCE_PREFIX. The recommended resource directory to be used as "
"prefix is ':/qt/qml/'. Specify AUTO_RESOURCE_PREFIX to use it."
__qt_internal_setup_policy(QTP0001 "6.5.0"
"':/qt/qml/' is the default resource prefix for QML modules. \
Check https://doc.qt.io/qt-6/qt-cmake-policy-qtp0001.html for policy details."
)
set(arg_RESOURCE_PREFIX "/")
qt6_policy(GET QTP0001 use_auto_prefix_policy)
if ("${use_auto_prefix_policy}" STREQUAL "NEW")
set(arg_RESOURCE_PREFIX "/qt/qml")
else()
set(arg_RESOURCE_PREFIX "/")
endif()
endif()
if(arg_NO_RESOURCE_TARGET_PATH)

View File

@ -1,7 +1,7 @@
# Copyright (C) 2022 The Qt Company Ltd.
# SPDX-License-Identifier: BSD-3-Clause
qt_add_library(extra_module STATIC)
qt_add_library(extra_module STATIC)
qt_add_qml_module(extra_module
URI "ExtraModule"
VERSION 1.0
@ -9,6 +9,7 @@ qt_add_qml_module(extra_module
Extra.qml
SOURCES
extrathing.cpp extrathing.h
RESOURCE_PREFIX /
)
add_subdirectory(ExtraModule)

View File

@ -0,0 +1,36 @@
// Copyright (C) 2023 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GFDL-1.3-no-invariants-only
/*!
\page qt-cmake-policy-qtp0001.html
\ingroup qt-cmake-policies
\title QTP0001
\keyword qt_cmake_policy_qtp0001
\summary {':/qt/qml/' is the default resource prefix for QML modules.}
This policy was introduced in Qt 6.5. It changes where
\l{qt_add_qml_module}{qt_add_qml_module()} stores QML resources in
the resource system.
The \c OLD behavior of this policy is that, unless
\c AUTO_RESOURCE_PREFIX is set, the \c RESOURCE_PREFIX argument for
\c{qt_add_qml_module()} defaults to \c{":/"}.
The \c NEW behavior of this policy is that the \c RESOURCE_PREFIX argument
for \c{qt_add_qml_module()} defaults to \c{\":/qt/qml/"}. The new behavior
ensures that modules are put into the \l{QML Import Path} and can be
found without further setup.
Qt 6.5 issues warnings if you do not pass any of the following arguments to the
\c qt_add_qml_module command: \c RESOURCE_PREFIX, \c AUTO_RESOURCE_PREFIX,
\c NO_RESOURCE_TARGET_PATH. Use the \l qt_policy command to suppress
the warning by explicitly setting the policy to \c OLD or \c NEW.
\note The \c{OLD} behavior of a policy is deprecated, and may
be removed in the future.
\sa qt_policy, qt_cmake_policies, qt_add_qml_module
*/

View File

@ -29,4 +29,5 @@ qt_add_qml_module(qqc_test
"resources with space/main.qml"
RESOURCES
main.cpp
RESOURCE_PREFIX /
)

View File

@ -27,5 +27,6 @@ qt_add_qml_module(cmake_test
URI duck
VERSION 1.0
QML_FILES main.qml
RESOURCE_PREFIX /
)

View File

@ -8,5 +8,6 @@ qt_add_qml_module(duck_tick
VERSION 1.0
QML_FILES
Tick.qml
RESOURCE_PREFIX /
)

View File

@ -8,5 +8,6 @@ qt_add_qml_module(duck_trick
VERSION 1.0
QML_FILES
Trick.qml
RESOURCE_PREFIX /
)

View File

@ -9,4 +9,5 @@ qt_add_qml_module(BackingFirst
Neighbor.h
QML_FILES
DoSomething.qml
RESOURCE_PREFIX /
)

View File

@ -5,4 +5,5 @@ qt_add_qml_module(BackingSecond
URI Second
VERSION 1.0
QML_FILES CheckIt.qml
RESOURCE_PREFIX /
)