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
13
This policy was introduced in Qt 6.8. It causes the build system to generate
14
an extra qmldir file for each additional directory that contains QML files in
15
a QML module.
16
17
Enabling this policy ensures that the implicit import of each of the QML
18
components in your module is the same as the module itself. This means that
19
all the components can see each other without explicitly importing the module.
20
21
The \c OLD behavior of this policy is that a qmldir file is only generated for
22
the root directory of a module.
23
24
The \c NEW behavior of this policy is that for each directory with QML files in
25
a 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
27
resource location.
28
29
\section1 Background
30
31
The QML engine always performs an implicit import of the directory a QML file
32
resides in. For files in the module root directory, this implicit import
33
resolves to the module itself, so all sibling components are visible. However,
34
when QML files are placed in subdirectories, the implicit import resolves to
35
that subdirectory instead. Components in the subdirectory then cannot see other
36
components in the same module without adding an explicit \c import statement.
37
This is a frequent source of errors.
38
39
With the \c NEW behavior, the build system generates a qmldir file in each
40
subdirectory containing a \c prefer directive pointing to the module's canonical
41
resource location. This makes the implicit import of every QML file in the
42
module equivalent to importing the module itself, so all components can find
43
each other regardless of their directory.
44
45
\section1 Example
46
47
Consider a module with QML files split across multiple subdirectories:
48
49
\badcode
50
qt_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
59
A 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
64
With the \c NEW behavior, an extra qmldir is generated in both the \c controls
65
and \c views subdirectories. Each redirects the implicit import to the module's
66
canonical resource location, so \c{views/MyView.qml} can use \c{MyButton}
67
and any other type in the module without an explicit import.
68
69
You can set the policy explicitly in your project before calling
70
\c{qt_add_qml_module()}:
71
\badcode
72
qt_policy(SET QTP0004 NEW)
73
\endcode
74
Alternatively, call
75
\l{qt6_standard_project_setup}{qt_standard_project_setup()} with a
76
\c REQUIRES version of 6.8 or later.
77
78
Qt 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
*/
qtdeclarative
src
qml
doc
src
cmake
policy
qtp0004.qdoc
Generated on
for Qt by
1.16.1