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
qt_target_qml_sources.qdoc
Go to the documentation of this file.
1
// Copyright (C) 2021 The Qt Company Ltd.
2
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GFDL-1.3-no-invariants-only
3
4
/*!
5
\page qt-target-qml-sources.html
6
\ingroup cmake-commands-qtqml
7
8
\title qt_target_qml_sources
9
\keyword qt6_target_qml_sources
10
11
\brief Add qml files and resources to an existing QML module target.
12
13
\cmakecommandsince 6.2
14
15
\include cmake-find-package-qml.qdocinc
16
17
\section1 Synopsis
18
19
\badcode
20
qt_target_qml_sources(
21
target
22
[QML_FILES ...]
23
[RESOURCES ...]
24
[PREFIX resource_path]
25
[OUTPUT_TARGETS out_targets_var]
26
[NO_LINT]
27
[NO_CACHEGEN]
28
[NO_QMLDIR_TYPES]
29
)
30
31
\endcode
32
33
\versionlessCMakeCommandsNote qt6_target_qml_sources()
34
35
\section1 Description
36
37
\note This command requires CMake 3.19 or later.
38
39
\c{qt_target_qml_sources()} provides the ability to add more files to a QML
40
module after \l{qt6_add_qml_module}{qt_add_qml_module()} has been called.
41
Typically, you pass the set of \c{.qml} files and resources to
42
\l{qt6_add_qml_module}{qt_add_qml_module()} directly, but in some cases, it may
43
be desirable, or even necessary, to add files after
44
\l{qt6_add_qml_module}{qt_add_qml_module()} has been called. For example, you
45
may wish to add files conditionally based on an \c{if} statement expression,
46
or from subdirectories that will only be added if certain criteria are met.
47
You might want to add a set of files with different characteristics to the
48
others, such as a different resource prefix, or with linting and bytecode
49
compilation disabled. The \c{qt_target_qml_sources()} command enables these
50
scenarios.
51
52
\section1 Arguments
53
54
The \c target must be the backing target of a QML module, or if the QML module
55
has no separate backing target, it must be the module's plugin target.
56
57
\c QML_FILES is a list of \c{.qml}, \c{.js} and \c{.mjs} files to be added to
58
the QML module. This option has exactly the same effect as the \c QML_FILES
59
option of the \l{qt6_add_qml_module}{qt_add_qml_module()} command, including
60
the automatic compilation to bytecode and lint processing.
61
62
The \c NO_CACHEGEN and \c NO_LINT options also have the same effect as they do
63
for \l{qt6_add_qml_module}{qt_add_qml_module()}. They disable the bytecode
64
compilation and lint processing for the files listed with \c QML_FILES. This
65
behavior can also be specified just for individual files using
66
\l{qml-source-file-properties}{source file properties}.
67
68
\c NO_QMLDIR_TYPES prevents the \c QML_FILES from being added as types to the
69
generated \l{qmldir-autogeneration}{qmldir} file.
70
71
\c RESOURCES has exactly the same effect as the \c RESOURCES option of the
72
\l{qt6_add_qml_module}{qt_add_qml_module()} command. It provides a list of
73
files to be added to the \c target as ordinary resources. These files are
74
typically things like images, shaders, etc. that the QML code refers to in some
75
way.
76
77
\target PREFIX
78
Files added to the module via \c QML_FILES or \c RESOURCES will be placed under
79
the same resource prefix and target path as they would if they were added by the
80
\l{qt6_add_qml_module}{qt_add_qml_module()} command. This can be overridden by
81
providing a different location with the \c PREFIX option. The value following
82
the \c PREFIX keyword will be used directly, without appending any target path.
83
The final resource path of each file will be the prefix, plus the path of the
84
file below the \c CMAKE_CURRENT_SOURCE_DIR. The \l{QT_RESOURCE_ALIAS} source
85
file property can also be used to override that relative path.
86
87
\badcode
88
qt_add_qml_module(backing
89
URI Example
90
VERSION 1.0
91
RESOURCE_PREFIX /my.company.com/imports
92
)
93
94
qt_target_qml_sources(backing
95
QML_FILES special/First.qml
96
RESOURCES icons/logo.png
97
)
98
99
qt_target_qml_sources(backing
100
PREFIX /other.company.com/debugging
101
QML_FILES Inspector.qml
102
)
103
\endcode
104
105
In the above example, the \c backing target's resources will end up with the
106
following contents:
107
108
\list
109
\li \c{/my.company.com/imports/Example/special/First.qml}
110
\li \c{/my.company.com/imports/Example/icons/logo.png}
111
\li \c{/other.company.com/debugging/Inspector.qml}
112
\endlist
113
114
\c OUTPUT_TARGETS is also analogous to the same option for
115
\l{qt6_add_qml_module}{qt_add_qml_module()}. Use it to specify the name of a
116
variable in which to store any additional targets created for static builds.
117
If the \c target will be installed, these additional targets will also need to
118
be installed to satisfy linking requirements.
119
120
\target qml-source-file-properties
121
\section1 Source File Properties
122
123
A number of source file properties can be used to influence how each individual
124
\c{.qml} file is treated at various points in the QML module processing. These
125
override any higher level options specified in calls to
126
\c{qt_target_qml_sources()} or \l{qt6_add_qml_module}{qt_add_qml_module()}.
127
All of these properties need to be set before the files are added with either
128
of those two commands.
129
130
\c QT_QML_SKIP_QMLLINT can be set to \c TRUE on a source file to prevent it
131
from being included in the \l{qmllint-auto}{automatic qmllint processing}.
132
By default, all \c{.qml} files will be included in the target's lint run, but
133
this option can be used to exclude specific files.
134
135
\c QT_QML_SKIP_CACHEGEN does a similar thing, preventing a source file from
136
being compiled to byte code when this property is set to \c TRUE. Note that the
137
file will still be added to the \c target as a resource in uncompiled form
138
(see \l{qmlcachegen-auto}{Caching compiled QML sources}).
139
140
Set the \c QT_QML_SKIP_QMLDIR_ENTRY source file property to \c TRUE to prevent
141
that QML or JavaScript file from being added as a type to the QML module's
142
\c{qmldir} file (see \l{qmldir-autogeneration}{Auto-generating \c{qmldir} and
143
typeinfo files}). This would normally be used for a file that does not expose
144
a public type, such as a private JavaScript file. For JavaScript files with
145
upper case names that are neither ECMAScript modules nor declared as stateless
146
libraries via \c{.pragma library} you should consider this option. If you
147
include them in the \c{qmldir} file, they are re-evaluated in the scope of
148
every QML document that explicitly or implicitly imports the module they
149
belong to.
150
151
By default, when \l{qmldir-autogeneration}{generating the \c qmldir file}, a
152
single type entry will be generated for each \c{.qml} file that provides a type.
153
It will be given a version number \c{X.0} where \c{X} is the major version of
154
the QML module. If the QML module has any \c PAST_MAJOR_VERSIONS set, the same
155
pattern will be applied to those too, appending \c{X.0} for each past major
156
version \c{X}. For situations where a file needs to provide type entries for
157
a different set of versions instead (e.g. it was first added in a minor patch
158
version after the \c{.0} release), specify those versions in the source file's
159
\c QT_QML_SOURCE_VERSIONS property. One type entry will be created for each
160
version.
161
162
If the type that a \c{.qml} file provides is a singleton, set its
163
\c QT_QML_SINGLETON_TYPE property to \c TRUE. Similarly, the file's
164
\c QT_QML_INTERNAL_TYPE source property can be set to \c TRUE to indicate that
165
the type it provides is an internal one. The name of the type itself can also
166
be overridden using the \c QT_QML_SOURCE_TYPENAME property. All three of these
167
will be reflected in the file's type entries in the
168
\l{qmldir-autogeneration}{generated \c qmldir file}.
169
The source properties must be set before \l{qt_add_qml_module}{creating} the
170
module the singleton belongs to.
171
172
\target QT_RESOURCE_ALIAS
173
All files listed with \c QML_FILES or \c RESOURCES will be added to the
174
\c{target}'s resources. Their location in the resources consists of a base point
175
and a relative path. The base point defaults to the concatenation of the QML
176
module's resource prefix and its target path, but these can be overridden with
177
the \l PREFIX argument. The relative path will default to the path of the file
178
relative to the \c{target}'s \c SOURCE_DIR target property. This relative path
179
can be overridden by setting the \c QT_RESOURCE_ALIAS property on the source
180
file. This is commonly used to collect files from different directories and
181
have them appear in the resources under a common location.
182
However, since Qt 6.8, enabling \l{QTP0004} is often a better option if the goal
183
is that all files of the QML module can be found in the implicit import.
184
In cases where such a manual setup is still necessary, it is advisable to pass
185
\c {NO_GENERATE_EXTRA_QMLDIRS} to \c{qt_add_qml_module}, as the extra qmldirs do
186
not take resource file aliasing into consideration.
187
188
\target qt_target_qml_sources_example
189
\snippet cmake/qt_target_qml_sources/CMakeLists.txt 0
190
191
In the above example, the \c qt_target_qml_sources_example target's resources
192
will end up with the following contents:
193
194
\list
195
\li \c{/my.company.com/imports/Example/File.qml}
196
\li \c{/my.company.com/imports/Example/FunnySingleton.qml}
197
\li \c{/my.company.com/imports/Example/templates/File.qml}
198
\li \c{/my.company.com/imports/Example/some_old_thing.qml}
199
\li \c{/my.company.com/imports/Example/button-types.png}
200
\li \c{/my.company.com/imports/Example/doc/README.txt}
201
\endlist
202
203
The generated \c qmldir file will contain the following type entries:
204
205
\badcode
206
File 2.0 File.qml
207
singleton FunnySingleton 2.0 FunnySingleton.qml
208
OldThing 1.1 some_old_thing.qml
209
OldThing 2.0 some_old_thing.qml
210
\endcode
211
212
\note The source FunnySingleton.qml file must already contain
213
the \c {pragma Singleton} statement. Setting the \c QT_QML_SINGLETON_TYPE source
214
property does not automatically generate the pragma.
215
216
\snippet cmake/qt_target_qml_sources/FunnySingleton.qml 0
217
218
*/
qtdeclarative
src
qml
doc
src
cmake
qt_target_qml_sources.qdoc
Generated on
for Qt by
1.14.0