Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qtp0004.qdoc
Go to the documentation of this file.
1// Copyright (C) 2024 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GFDL-1.3-no-invariants-only
3
4/*!
5\page qt-cmake-policy-qtp0004.html
6\ingroup qt-cmake-policies
7
8\title QTP0004
9\keyword qt_cmake_policy_qtp0004
10
11\summary {Extra directories with QML files in a QML module need extra qmldir files.}
12
13This policy was introduced in Qt 6.8. It causes the build system to generate
14an extra qmldir file for each additional directory that contains QML files in
15a QML module.
16
17Enabling this policy ensures that the implicit import of each of the QML
18components in your module is the same as the module itself. This means that
19all the components can see each other without explicitly importing the module.
20
21The \c OLD behavior of this policy is that a qmldir file is only generated for
22the root directory of a module.
23
24The \c NEW behavior of this policy is that for each directory with QML files in
25a module a separate qmldir file is generated. Each generated qmldir contains a
26\c prefer directive that redirects the implicit import to the module's canonical
27resource location.
28
29\section1 Background
30
31The QML engine always performs an implicit import of the directory a QML file
32resides in. For files in the module root directory, this implicit import
33resolves to the module itself, so all sibling components are visible. However,
34when QML files are placed in subdirectories, the implicit import resolves to
35that subdirectory instead. Components in the subdirectory then cannot see other
36components in the same module without adding an explicit \c import statement.
37This is a frequent source of errors.
38
39With the \c NEW behavior, the build system generates a qmldir file in each
40subdirectory containing a \c prefer directive pointing to the module's canonical
41resource location. This makes the implicit import of every QML file in the
42module equivalent to importing the module itself, so all components can find
43each other regardless of their directory.
44
45\section1 Example
46
47Consider a module with QML files split across multiple subdirectories:
48
49\badcode
50qt_add_qml_module(mymodule
51 URI MyModule
52 QML_FILES
53 Main.qml
54 controls/MyButton.qml
55 views/MyView.qml
56)
57\endcode
58
59A common need is for \c{views/MyView.qml} to use \c{MyButton} from the
60\c controls subdirectory. With the \c OLD behavior this fails silently:
61\c{views/MyView.qml} implicitly imports the \c views subdirectory, so
62\c{MyButton} is not found unless you add \c{import MyModule} explicitly.
63
64With the \c NEW behavior, an extra qmldir is generated in both the \c controls
65and \c views subdirectories. Each redirects the implicit import to the module's
66canonical resource location, so \c{views/MyView.qml} can use \c{MyButton}
67and any other type in the module without an explicit import.
68
69You can set the policy explicitly in your project before calling
70\c{qt_add_qml_module()}:
71\badcode
72qt_policy(SET QTP0004 NEW)
73\endcode
74Alternatively, call
75\l{qt6_standard_project_setup}{qt_standard_project_setup()} with a
76\c REQUIRES version of 6.8 or later.
77
78Qt 6.8 issues warnings if you do not explicitly set the policy.
79
80\qtpolicydeprecatedbehavior
81
82\sa qt_policy, {qt6_standard_project_setup}{qt_standard_project_setup()},
83 qt_cmake_policies, qt_add_qml_module
84
85*/