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_add_qml_module.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-add-qml-module.html
6
\ingroup cmake-commands-qtqml
7
8
\title qt_add_qml_module
9
\keyword qt6_add_qml_module
10
11
\summary{Defines a QML module.}
12
13
\cmakecommandsince 6.2
14
15
\include cmake-find-package-qml.qdocinc
16
17
\section1 Synopsis
18
19
\badcode
20
qt_add_qml_module(
21
target
22
URI uri
23
[VERSION version]
24
[PAST_MAJOR_VERSIONS ...]
25
[STATIC | SHARED]
26
[PLUGIN_TARGET plugin_target]
27
[OUTPUT_DIRECTORY output_dir]
28
[RESOURCE_PREFIX resource_prefix]
29
[CLASS_NAME class_name]
30
[TYPEINFO typeinfo]
31
[IMPORTS ...]
32
[OPTIONAL_IMPORTS ...]
33
[DEFAULT_IMPORTS ...]
34
[DEPENDENCIES ...]
35
[IMPORT_PATH ...]
36
[SOURCES ...]
37
[QML_FILES ...]
38
[RESOURCES ...]
39
[OUTPUT_TARGETS out_targets_var]
40
[DESIGNER_SUPPORTED]
41
[FOLLOW_FOREIGN_VERSIONING]
42
[NAMESPACE namespace]
43
[NO_PLUGIN]
44
[NO_PLUGIN_OPTIONAL]
45
[NO_CREATE_PLUGIN_TARGET]
46
[NO_GENERATE_PLUGIN_SOURCE]
47
[NO_GENERATE_QMLTYPES]
48
[NO_GENERATE_QMLDIR]
49
[NO_GENERATE_EXTRA_QMLDIRS]
50
[NO_GENERATE_QTCONF]
51
[NO_LINT]
52
[NO_CACHEGEN]
53
[NO_RESOURCE_TARGET_PATH]
54
[NO_IMPORT_SCAN]
55
[DISCARD_QML_CONTENTS]
56
[ENABLE_TYPE_COMPILER]
57
[TYPE_COMPILER_NAMESPACE namespace]
58
[QMLTC_EXPORT_DIRECTIVE export_macro]
59
[QMLTC_EXPORT_FILE_NAME header_defining_export_macro]
60
61
)
62
63
\endcode
64
65
\versionlessCMakeCommandsNote qt6_add_qml_module()
66
67
See \l {Building a QML application} and \l {Building a reusable QML module}
68
for examples that define QML modules.
69
70
\section1 Description
71
72
This command defines a QML module that can consist of C++ sources, \c{.qml}
73
files, or both. It ensures that essential module details are provided and that
74
they are consistent. It also sets up and coordinates things like cached
75
compilation of \c{.qml} sources, resource embedding, linting checks, and
76
auto-generation of some key module files.
77
78
\section2 Target Structure
79
80
A QML module can be structured in a few different ways. The following scenarios
81
are the typical arrangements:
82
83
\section3 Separate backing and plugin targets
84
85
This is the recommended arrangement for most QML modules. All of the module's
86
functionality is implemented in the \e backing target, which is given as the
87
first command argument. C++ sources, \c{.qml} files, and resources should all
88
be added to the backing target. The backing target is a library that should be
89
installed in the same location as any other library defined by the project.
90
91
The source directory structure under which the backing target is created should
92
match the target path of the QML module (the target path is the module's URI
93
with dots replaced by forward slashes). If the source directory structure
94
doesn't match the target path, \c{qt_add_qml_module()} will issue a warning.
95
96
The following example shows a suitable source directory structure for a QML
97
module with a URI of \c{MyThings.Panels}. The call to \c{qt_add_qml_module()}
98
would be in the \c{CMakeLists.txt} file shown.
99
100
\badcode
101
src
102
+-- MyThings
103
+-- Panels
104
+-- CMakeLists.txt
105
\endcode
106
107
A separate \e plugin target is associated with the QML module. It is used at
108
runtime to load the module dynamically when the application doesn't already
109
link to the backing target. The plugin target will also be a library and is
110
normally installed to the same directory as the module's
111
\l{Module Definition qmldir Files}{qmldir} file.
112
113
The plugin target should ideally contain nothing more than a trivial
114
implementation of the plugin class. This allows the plugin to be designated as
115
optional in the \c qmldir file. Other targets can then link directly to the
116
backing target and the plugin will not be needed at runtime, which can improve
117
load-time performance. By default, a C++ source file that defines a minimal
118
plugin class will be automatically generated and added to the plugin target.
119
For cases where the QML module needs a custom plugin class implementation, the
120
\l{NO_GENERATE_PLUGIN_SOURCE} and usually the \l{NO_PLUGIN_OPTIONAL} options
121
will be needed.
122
123
The \c STATIC QML modules also generate the static QML plugins if
124
\c NO_PLUGIN is not specified. Targets that import such \c STATIC QML modules
125
also need to explicitly link to corresponding QML plugins.
126
127
\note
128
When using static linking, it might be necessary to use
129
\l {Q_IMPORT_QML_PLUGIN} to ensure that the QML plugin is correctly linked.
130
131
\section3 Plugin target with no backing target
132
133
A QML module can be defined with the plugin target serving as its own backing
134
target. In this case, the module must be loaded dynamically at runtime and
135
cannot be linked to directly by other targets. To create this arrangement,
136
the \c PLUGIN_TARGET keyword must be used, with the \c target repeated as the
137
plugin target name. For example:
138
139
\badcode
140
qt_add_qml_module(someTarget
141
PLUGIN_TARGET someTarget
142
...
143
)
144
\endcode
145
146
While this arrangement may seem marginally simpler to deploy, a separate
147
backing target should be preferred where possible due to the potentially better
148
load-time performance.
149
150
\section3 Executable as a QML module
151
152
An executable target can act as a backing target for a QML module. In this case,
153
there will be no plugin library, since the QML module will always be loaded
154
directly as part of the application. The \c{qt_add_qml_module()} command will
155
detect when an executable is used as the backing target and will automatically
156
disable the creation of a separate plugin. Do not use any of the options with
157
\c{PLUGIN} in their name when using this arrangement.
158
159
When an executable is used as the backing target, the source directory structure
160
is not expected to match the QML module's target path.
161
See \l{qmlcachegen-auto}{Caching compiled QML sources} for additional target
162
path differences for compiled-in resources.
163
164
165
\target qmldir-autogeneration
166
\section2 Auto-generating \c{qmldir} and typeinfo files
167
168
By default, a \l{Module Definition qmldir Files}{qmldir} file and a typeinfo
169
file will be auto-generated for the QML module being defined. The contents of
170
those files are determined by the various arguments given to this command, as
171
well as the sources and \c{.qml} files added to the backing target.
172
The \l OUTPUT_DIRECTORY argument determines where the \c qmldir and typeinfo
173
files will be written to. If the QML module has a plugin, that plugin will also
174
be created in the same directory as the \c qmldir file.
175
176
If \l{QTP0004} policy is set to \c NEW, for each further directory that contains
177
\c{.qml} files another \c qmldir file is generated. These extra \c qmldir files
178
merely redirect to the module's base directory via a \c prefer directive. This
179
is so that all the QML components in a module can access each other, no matter
180
which directory they are stored in.
181
182
If using a statically built Qt, the backing target's \c{.qml} files will be
183
scanned during the CMake configure run to determine the imports used by the
184
module and to set up linking relationships (the \c{NO_IMPORT_SCAN} keyword
185
can be given to disable this). When a \c{.qml} file is added to or
186
removed from the module, CMake will normally re-run automatically and the
187
relevant files will be re-scanned, since a \c{CMakeLists.txt} file will have
188
been modified. During the course of development, an existing \c{.qml} file may
189
add or remove an import or a type. On its own, this would not cause CMake to
190
re-run automatically, so you should explicitly re-run CMake to force the
191
\c qmldir file to be regenerated and any linking relationships to be updated.
192
193
The backing target's C++ sources are scanned at build time to generate a
194
typeinfo file and a C++ file to register the associated types. The generated
195
C++ file is automatically added to the backing target as a source.
196
This requires \c AUTOMOC to be enabled on the target. The project is
197
responsible for ensuring this, usually by setting the \c CMAKE_AUTOMOC variable
198
to \c TRUE before calling \c qt_add_qml_module(), or by passing in an existing
199
target with the \c AUTOMOC target property already set to \c TRUE. It isn't an
200
error to have \c AUTOMOC disabled on the target, but the project is then
201
responsible for handling the consequences. This may include having to manually
202
generate the typeinfo file instead of allowing it to be auto-generated with
203
missing details, and adding C++ code to register the types.
204
205
Projects should prefer to use the auto-generated typeinfo and \c qmldir files
206
where possible. They are easier to maintain and they don't suffer from the same
207
susceptibility to errors that hand-written files do. Nevertheless, for
208
situations where the project needs to provide these files itself, the
209
auto-generation can be disabled. The \c NO_GENERATE_QMLDIR option disables the
210
\c qmldir auto-generation and the \c NO_GENERATE_QMLTYPES option disables the
211
typeinfo and C++ type registration auto-generation. If the auto-generated
212
typeinfo file is acceptable, but the project wants to use a different name for
213
that file, it can override the default name with the \c TYPEINFO option (but
214
this should not typically be needed).
215
216
\target qmlcachegen-auto
217
\section2 Caching compiled QML sources
218
219
All \c{.qml}, \c{.js}, and \c{.mjs} files added to the module via the
220
\c QML_FILES argument will be compiled to bytecode and cached directly in the
221
backing target. This improves load-time performance of the module. The original
222
uncompiled files are also stored in the backing target's resources, as these
223
may still be needed in certain situations by the QML engine.
224
225
The resource path of each file is determined by its path relative to the
226
current source directory (\c CMAKE_CURRENT_SOURCE_DIR). This resource path is
227
appended to a prefix formed by concatenating the \l{RESOURCE_PREFIX} and
228
the target path (but see \l NO_RESOURCE_TARGET_PATH for an exception to this).
229
230
If \l{QTP0001} policy is set to \c NEW, the \l{RESOURCE_PREFIX} defaults
231
to \c{/qt/qml/} which is the default import path of the QML engine.
232
This ensures that modules are put into the \l{QML Import Path} and can be
233
found without further setup.
234
235
Ordinarily, the project should aim to place \c{.qml} files in
236
the same relative location as they would have in the resources. If the \c{.qml}
237
file is in a different relative directory to its desired resource path, its
238
location in the resources needs to be explicitly specified. This is done by
239
setting the \c QT_RESOURCE_ALIAS source file property, which must be set before
240
the \c{.qml} file is added. For example:
241
242
\badcode
243
set_source_files_properties(path/to/somewhere/MyFrame.qml PROPERTIES
244
QT_RESOURCE_ALIAS MyFrame.qml
245
)
246
247
qt_add_qml_module(someTarget
248
URI MyCo.Frames
249
RESOURCE_PREFIX /my.company.com/imports
250
QML_FILES
251
path/to/somewhere/MyFrame.qml
252
AnotherFrame.qml
253
)
254
\endcode
255
256
In the above example, the target path will be \c{MyCo/Frames}. After
257
taking into account the source file properties, the two \c{.qml} files will be
258
found at the following resource paths:
259
260
\list
261
\li \c{/my.company.com/imports/MyCo/Frames/MyFrame.qml}
262
\li \c{/my.company.com/imports/MyCo/Frames/AnotherFrame.qml}
263
\endlist
264
265
In the rare case that you want to override the automatic selection of the
266
qmlcachegen program to be used, you may set the \c QT_QMLCACHEGEN_EXECUTABLE
267
target property on the module target. For example:
268
269
\badcode
270
set_target_properties(someTarget PROPERTIES
271
QT_QMLCACHEGEN_EXECUTABLE qmlcachegen
272
)
273
\endcode
274
275
This explicitly selects qmlcachegen as the program to be used, even if
276
better alternatives are available.
277
278
Furthermore, you can pass extra arguments to qmlcachegen, by setting the
279
\c QT_QMLCACHEGEN_ARGUMENTS option. In particular, the \c --only-bytecode
280
option will turn off compilation of QML script code to C++. For example:
281
282
\badcode
283
set_target_properties(someTarget PROPERTIES
284
QT_QMLCACHEGEN_ARGUMENTS "--only-bytecode"
285
)
286
\endcode
287
288
Another important argument is \c{--direct-calls}. You can use it to enable the
289
direct mode of \l{The QML script compiler} in case the Qt Quick Compiler
290
Extensions are installed. If the extensions are not installed, the argument is
291
ignored. There is a shorthand called \c {QT_QMLCACHEGEN_DIRECT_CALLS} for it.
292
293
\badcode
294
set_target_properties(someTarget PROPERTIES
295
QT_QMLCACHEGEN_DIRECT_CALLS ON
296
)
297
\endcode
298
299
Finally, the \c --verbose argument can be used to see diagnostic output from
300
qmlcachegen:
301
302
\badcode
303
set_target_properties(someTarget PROPERTIES
304
QT_QMLCACHEGEN_ARGUMENTS "--verbose"
305
)
306
\endcode
307
308
With this flag set, qmlcachegen will output warnings for each function it
309
cannot compile to C++. Some of these warnings will point to problems in your
310
QML code and some will tell you that certain features of the QML language are
311
not implemented in the C++ code generator. In both cases, qmlcachegen will
312
still generate byte code for such functions. If you want to see only the
313
problems in your QML code, you should use qmllint and the targets generated
314
for it instead.
315
316
\target qmllint-auto
317
\section2 Linting QML sources
318
319
A separate linting target will be automatically created if any \c{.qml} files
320
are added to the module via the \c QML_FILES keyword, or by a later call to
321
\l{qt6_target_qml_sources}{qt_target_qml_sources()}. The name of the linting
322
target will be the \c target followed by \c{_qmllint}. An \c{all_qmllint}
323
target which depends on all the individual \c{*_qmllint} targets is also
324
provided as a convenience.
325
326
A \c dump_qml_context_properties global build target is automatically created
327
that runs \l qmlcontextpropertydump when no qml context property dump file
328
already exists. \l qmlcontextpropertydump creates a qml context property dump file
329
that is read by \l qmllint to warn about usages of context properties in QML.
330
331
A \c clean_qml_context_properties global build target allows to delete an already
332
existing qml context property dump file, and can be used to recompute the qml context
333
property dump file on a future build of the \c dump_qml_context_properties global build
334
target.
335
336
Linting targets depend on the \c dump_qml_context_properties build target when
337
the \l QT_QMLLINT_CONTEXT_PROPERTY_DUMP variable is enabled.
338
339
340
\target qml-naming-js-files
341
\section2 Naming conventions for \c{.js} files
342
343
JavaScript file names that are intended to be addressed as components should
344
start with an uppercase letter.
345
346
Alternatively, you may use lowercase file names and set the source file
347
property \l QT_QML_SOURCE_TYPENAME to the desired type name.
348
349
\target qml-cmake-singletons
350
\section2 Singletons
351
352
If a QML module has \c{.qml} files which provide singleton types, these files
353
need to have their \c QT_QML_SINGLETON_TYPE source property set to \c TRUE, to
354
ensure that the \c singleton command is written into the
355
\l{Module Definition qmldir Files}{qmldir} file. This must be done in addition
356
to the QML file containing the \c {pragma Singleton} statement.
357
The source property must be set before creating the module the
358
singleton belongs to.
359
360
See \l{qt_target_qml_sources_example}{qt_target_qml_sources()} for an example on
361
how to set the \c QT_QML_SINGLETON_TYPE property.
362
363
\target qmltc-cmake
364
\section2 Compiling QML to C++ with QML type compiler
365
366
\note The \l{QML type compiler} \c{qmltc} does not guarantee that the generated
367
C++ stays API-, source- or binary-compatible between past or future versions,
368
even patch versions.
369
Furthermore, qmltc-compiled apps using Qt's QML modules will require linking
370
against private Qt API, see also
371
\l{QML type compiler#compiling-qml-code-with-qmltc}{Compiling QML code with qmltc}.
372
373
374
If a QML module has \c{.qml} files, you can compile them to C++ using \l{QML
375
type compiler}{qmltc}. Unlike \l{qmlcachegen-auto}{bytecode compilation}, you
376
have to explicitly enable qmltc via \l{ENABLE_TYPE_COMPILER} argument. In which
377
case, \c{.qml} files specified under \c{QML_FILES} would be compiled. Files
378
ending with \c{.js} and \c{.mjs} are ignored as qmltc does not compile
379
JavaScript code. Additionally, files marked with QT_QML_SKIP_TYPE_COMPILER
380
source file property are also skipped.
381
382
By default, qmltc creates lower-case \c{.h} and \c{.cpp} files for a given
383
\c{.qml} file. For example, \c{Foo.qml} ends up being compiled into \c{foo.h}
384
and \c{foo.cpp}.
385
386
The created C++ files are placed into a dedicated \c{.qmltc/<target>/}
387
sub-directory of the \c BINARY_DIR of the \c target. These files are then
388
automatically added to the target sources and compiled as Qt C++ code along with
389
other source files.
390
391
While processing QML_FILES, the following source file properties are respected:
392
\list
393
\li \c{QT_QMLTC_FILE_BASENAME}: use this source file property to specify a
394
non-default .h and .cpp file name, which might be useful to e.g. resolve
395
conflicting file names (imagine you have main.qml that is being
396
compiled, but main.h already exists, so #include "main.h" might not do
397
what you expect it to do). QT_QMLTC_FILE_BASENAME is expected to be a
398
file name (without extension), so any preceding directory is ignored.
399
Unlike in the case of default behavior, the QT_QMLTC_FILE_BASENAME is
400
not lower-cased.
401
\li \c{QT_QML_SKIP_TYPE_COMPILER}: use this source file property to
402
specify that a QML file must be ignored by qmltc.
403
\endlist
404
405
\section1 Arguments
406
407
\section2 Required arguments
408
409
The \c target specifies the name of the backing target for the QML module.
410
By default, it is created as a shared library if Qt was built as shared
411
libraries, or as a static library otherwise. This choice can be explicitly
412
overridden with the \c STATIC or \c SHARED options.
413
414
Every QML module must define a \c URI. It should be specified in dotted URI
415
notation, such as \c{QtQuick.Layouts}. Each segment must be a well-formed
416
ECMAScript Identifier Name. This means, for example, the segments
417
must not start with a number and they must not contain \e{-} (minus)
418
characters. As the \c URI will be translated into directory names, you
419
should restrict it to alphanumeric characters of the latin alphabet,
420
underscores, and dots. Other QML modules may use this name in
421
\l{qtqml-syntax-imports.html}{import statements} to import the module. The
422
\c URI will be used in the \c module line of the generated
423
\l{Module Definition qmldir Files}{qmldir} file. The \c URI is also used to
424
form the \e{target path} by replacing dots with forward slashes.
425
426
See \l{qtqml-modules-identifiedmodules.html}{Identified Modules} for further
427
in-depth discussion of the module URI.
428
429
\section2 Versions
430
431
A QML module can also define a \c VERSION in the form \c{Major.Minor}, where
432
both \c Major and \c Minor must be integers. An additional \c{.Patch}
433
component may be appended, but will be ignored. A list of earlier major
434
versions the module provides types for can also optionally be given after the
435
\c PAST_MAJOR_VERSIONS keyword (see below).
436
See \l{qtqml-modules-identifiedmodules.html}{Identified Modules} for further
437
in-depth discussion of version numbering,
438
\l{Registering past major versions} for registering past major versions, and
439
\l{Keeping module versions in sync} for keeping module versions in sync.
440
441
If you don't need versions you should omit the \c VERSION argument. It defaults
442
to the highest possible version. Internal versioning of QML modules has some
443
fundamental flaws. You should use an external package management mechanism to
444
manage different versions of your QML modules.
445
446
\section2 Adding sources and resources to the module
447
448
\note A QML module is a logically grouped, self-contained unit of functionality.
449
All files that make up the module should reside in the same directory as the CMakeLists.txt
450
defining the module or in one of its subdirectories.
451
If a specific functionality is required in multiple modules, consider encapsulating it within
452
a separate module. This module can then be imported into other modules, promoting code reuse
453
and maintainability.
454
455
\c SOURCES specifies a list of non-QML sources to be added to the backing
456
target. It is provided as a convenience and is equivalent to adding the sources
457
to the backing target with the built-in \c{target_sources()} CMake command.
458
459
\c QML_FILES lists the \c{.qml}, \c{.js} and \c{.mjs} files for the module.
460
These will be automatically \l{qmlcachegen-auto}{compiled into bytecode} and
461
embedded in the backing target unless the \c NO_CACHEGEN option is given.
462
The uncompiled file is always stored in the embedded resources of the backing
463
target, even if \c NO_CACHEGEN is specified. Unless the \c NO_LINT option is
464
given, the uncompiled files will also be
465
\l{Linting QML sources}{processed by \c qmllint} via a separate custom build
466
target. The files will also be used to populate type information in the
467
generated \l{Module Definition qmldir Files}{qmldir} file by default.
468
\c NO_GENERATE_QMLDIR can be given to disable the automatic generation of the
469
\c qmldir file. This should normally be avoided, but for cases where the
470
project needs to provide its own \c qmldir file, this option can be used.
471
Since Qt 6.8, when \l{QTP0004} is enabled, \c qt_add_qml_module will
472
create additional \c qmldir files for each subdirectory in the QML module,
473
which ensure that each QML file will import its own module via the implicit
474
import. This behavior can be turned off for a QML module by passing the
475
\c NO_GENERATE_EXTRA_QMLDIRS flag to it.
476
The \c NO_GENERATE_QMLDIR implies \c NO_GENERATE_EXTRA_QMLDIRS.
477
478
\note See \l{qt6_target_qml_sources}{qt_target_qml_sources()} for further details on
479
how to add qmlfiles after \c qt_add_qml_module() was called.
480
For example, you may wish to add files conditionally based on an if statement
481
expression, or from subdirectories that will only be added if certain criteria
482
are met.
483
Furthermore, files added with \l{qt6_target_qml_sources}{qt_target_qml_sources()}
484
also can specify if they should be skipped for the linting,
485
\l{qmlcachegen-auto}{bytecode compilation} or \c qmldir file generation.
486
487
\c RESOURCES lists any other files needed by the module, such as images
488
referenced from the QML code. These files will be added as compiled-in
489
resources (see \l RESOURCE_PREFIX for an explanation of the base point they
490
will be located under). If needed, their relative location can
491
be controlled by setting the \c QT_RESOURCE_ALIAS source property, just as for
492
\c{.qml} files (see \l{qmlcachegen-auto}{Caching compiled QML sources}).
493
494
\target RESOURCE_PREFIX
495
\c RESOURCE_PREFIX is intended to encapsulate a namespace for the project and
496
will often be the same for all QML modules that the project defines.
497
498
However, it is better to set the \l QTP0001 CMake policy instead. It defines a
499
default resource prefix that ensures that your QML module ends
500
up under one of the QML engine's default \l[QtQml]{QML Import Path}{import paths}.
501
502
If you set a \c RESOURCE_PREFIX, you should also add it to the
503
\l[QtQml]{QML Import Path}{import paths} for the QML Engine to find the QML module.
504
505
If \l QTP0001 is enabled (e.g. via
506
\c {qt_standard_project_setup(REQUIRES 6.5)}), the default value is
507
\c "/qt/qml/", otherwise it is \c {"/"}.
508
509
\target NO_RESOURCE_TARGET_PATH
510
When various files are added to the compiled-in resources, they are placed
511
under a path formed by concatenating the \c RESOURCE_PREFIX and the target path.
512
For the special case where the backing target is an executable, it may be
513
desirable to place the module's \c{.qml} files and other resources directly
514
under the \c RESOURCE_PREFIX instead. This can be achieved by specifying the
515
\c NO_RESOURCE_TARGET_PATH option, which may only be used if the backing target
516
is an executable.
517
518
\target PAST_MAJOR_VERSIONS
519
\section2 Registering past major versions
520
521
\c PAST_MAJOR_VERSIONS contains a list of additional major version that the module
522
provides. For each of those versions and each QML file
523
without a \c QT_QML_SOURCE_VERSIONS setting an additional entry in the
524
\l{Module Definition qmldir Files}{qmldir} file will be generated to specify
525
the extra version. Furthermore, the generated module registration code will
526
register the past major versions using \l{qmlRegisterModule()} on the C++ side.
527
The module registration code is automatically generated for your QML module,
528
unless you specify \c{NO_GENERATE_QMLTYPES} (but use of this option is strongly
529
discouraged). Usage of \c PAST_MAJOR_VERSIONS adds some overhead when your
530
module is imported. You should increment the major version of your module as
531
rarely as possible. Once you can rely on all QML files importing this module to
532
omit the version in their imports, you can safely omit \c{PAST_MAJOR_VERSIONS}.
533
All the QML files will then import the latest version of your module. If you
534
have to support versioned imports, consider supporting only a limited number of
535
past major versions.
536
537
\section2 Declaring module dependencies
538
539
\target IMPORTS
540
\c IMPORTS provides a list of other QML modules that this module imports. Each
541
module listed here will be added as an \c{import} entry in the generated
542
\l{Module Definition qmldir Files}{qmldir} file. If a QML file imports
543
this module, it also imports all the modules listed under \c{IMPORTS}.
544
Optionally, a version can be specified by appending it after a slash, such as
545
\c{QtQuick/2.0}. Omitting the version will cause the greatest version available
546
to be imported. You may only specify the major version, as in \c{QtQuick/2}. In
547
that case the greatest minor version available with the given major version will
548
be imported. Finally, \c{auto} may be given as version (\c{QtQuick/auto}). If
549
\c{auto} is given, the version that the current module is being imported with is
550
propagated to the module to be imported. Given an entry \c{QtQuick/auto} in a
551
module \c{YourModule}, if a QML file specifies \c{import YourModule 3.14}, this
552
results in importing version \c{3.14} of \c{QtQuick}. For related modules that
553
follow a common versioning scheme, you should use \c{auto}.
554
555
Entries in \c IMPORTS may also refer to CMake targets by prefixing the target
556
name with the \c TARGET keyword. In this case, the QML module URI is determined
557
automatically from the target. This requires opting into the CMake policy
558
\l{QTP0005}.
559
560
For example, a QML module can import another module by referring to the
561
CMake target that provides it:
562
563
\badcode
564
qt_add_qml_module(my_module
565
URI MyModule
566
VERSION 1.0
567
IMPORTS
568
TARGET OtherQmlModule
569
)
570
\endcode
571
572
\c OPTIONAL_IMPORTS provides a list of other QML modules that this module
573
\e may import at run-time. These are not automatically imported by the QML
574
engine when importing the current module, but rather serve as hints to tools
575
like \c qmllint. Versions can be specified in the same way as for \c IMPORTS.
576
Each module listed here will be added as an \c{optional import} entry in the
577
generated \l{Module Definition qmldir Files}{qmldir} file.
578
579
Entries in \c OPTIONAL_IMPORTS may also refer to CMake targets by prefixing the
580
target name with the \c TARGET keyword. In this case, the QML module URI is
581
determined automatically from the target. This requires opting into the CMake
582
policy \l{QTP0005}.
583
584
For example:
585
586
\badcode
587
qt_add_qml_module(my_module
588
URI MyModule
589
VERSION 1.0
590
OPTIONAL_IMPORTS
591
TARGET OptionalQmlModule
592
)
593
\endcode
594
595
\c DEFAULT_IMPORTS specifies which of the optional imports are the default entries
596
that should be loaded by tooling. One entry should be specified for every group of
597
\c OPTIONAL_IMPORTS in the module. As optional imports are only resolved at runtime,
598
tooling like qmllint cannot in general know which of the optional imports should
599
be resolved. To remedy this, you can specify one of the optional imports as the
600
default import; tooling will then pick it. If you have one optional import that
601
gets used at runtime without any further configuration, that is an ideal candidate
602
for the default import.
603
604
Entries in \c DEFAULT_IMPORTS may also refer to CMake targets by prefixing the
605
target name with the \c TARGET keyword. In this case, the QML module URI is
606
determined automatically from the target. This requires opting into the CMake
607
policy \l{QTP0005}.
608
609
For example:
610
611
\badcode
612
qt_add_qml_module(my_module
613
URI MyModule
614
VERSION 1.0
615
OPTIONAL_IMPORTS
616
TARGET BackendA
617
TARGET BackendB
618
DEFAULT_IMPORTS
619
TARGET BackendA
620
)
621
\endcode
622
623
\c DEPENDENCIES provides a list of other QML modules that this module depends
624
on, but doesn't necessarily import. It would typically be used for dependencies
625
that only exist at the C++ level, such as a module registering a class to QML
626
which uses or inherits C++ defined types defined in another module.
627
628
For example, if one would like to subclass \c QQuickItem as following:
629
630
\badcode
631
class MyItem: public QQuickItem { ... };
632
\endcode
633
634
then one has to make sure that the module containing \c QQuickItem, called
635
\c QtQuick, is declared as a dependency via the \c DEPENDENCIES option:
636
637
\badcode
638
qt_add_qml_module(myTarget
639
...
640
DEPENDENCIES QtQuick
641
)
642
\endcode
643
644
Not doing so might result in linting errors, errors during type compilation
645
with \l{QML type compiler}{qmltc} or during binding and function compilation to C++
646
with \l{qmlcachegen-auto}{qmlcachegen}.
647
648
Another example might be:
649
\code
650
class MyComponent : public QObject {
651
Q_OBJECT
652
QML_ELEMENT
653
654
// ...
655
656
signals:
657
void sigZoomAtMousePosition(const QPointF& aMousePos, double aZoomScaleFactor);
658
};
659
\endcode
660
where \c{DEPENDENCIES QtQml} is required for QML tooling to find and use QPointF:
661
\badcode
662
qt_add_qml_module(myTarget
663
...
664
DEPENDENCIES QtQml
665
)
666
\endcode
667
668
Entries in \c DEPENDENCIES may also refer to CMake targets by prefixing the
669
target name with the \c TARGET keyword. In this case, the QML module URI and
670
import path are determined automatically from the target. This requires
671
opting into the CMake policy \l{QTP0005}.
672
673
For example, a QML module can declare a dependency on another module by
674
referring to the CMake target that provides it:
675
676
\badcode
677
qt_add_qml_module(my_module
678
URI MyModule
679
VERSION 1.0
680
DEPENDENCIES
681
TARGET OtherQmlModule
682
)
683
\endcode
684
685
\note Using \c{TARGET <cmake-target>} in \c DEPENDENCIES, \c IMPORTS,
686
\c OPTIONAL_IMPORTS, or \c DEFAULT_IMPORTS does not automatically link against
687
the given target. It is used only to derive QML metadata, import paths, and to
688
establish build-order dependencies. If the QML module or its generated plugin
689
requires symbols from that target, the target must still be linked explicitly
690
using \c target_link_libraries().
691
692
\note The \c{<cmake-target>} used with the \c TARGET keyword must be a target
693
built by your project (not an imported target provided by \c{find_package}).
694
695
\note When using \c{TARGET <cmake-target>} in \c DEPENDENCIES, \c IMPORTS,
696
\c OPTIONAL_IMPORTS, or \c DEFAULT_IMPORTS, only the direct dependency on the
697
specified target is established. Transitive QML module dependencies of the
698
referenced target (that is, QML modules which that target itself depends on)
699
are \e not automatically added. Each QML module dependency must be declared
700
explicitly.
701
702
\note Adding the module to \c DEPENDENCIES is not necessary if the module
703
is already imported via the \c IMPORTS option. The recommended way is to
704
use the lighter alternative \c DEPENDENCIES over \c IMPORTS.
705
706
\target NO_GENERATE_QTCONF
707
When the backing target is an executable and \c{TARGET} dependencies are used
708
(requires \l{QTP0005}), \c{qt_add_qml_module()} automatically generates a
709
\c{qt.conf} file next to the executable in the build tree. This file configures
710
the \l{QML Import Path} so that the QML engine can locate the module's
711
dependencies at run time without requiring manual \c IMPORT_PATH entries. The
712
\c NO_GENERATE_QTCONF option suppresses this behavior, which may be necessary
713
if the generated file conflicts with a \c qt.conf already present in the build
714
directory. This option was introduced in Qt 6.12.
715
716
\warning When using \c NO_GENERATE_QTCONF, the application may fail to find its
717
QML module dependencies at run time. You must manually configure the
718
\l{QML Import Path} — for example, by adding the appropriate import paths to
719
the existing \c qt.conf file.
720
721
The module version of the
722
dependencies must be specified along with the module name, in the same form as
723
used for \c IMPORTS and \c OPTIONAL_IMPORTS. Each module listed here will be
724
added as a \c{depends} entry in the generated
725
\l{Module Definition qmldir Files}{qmldir} file.
726
727
\target IMPORT_PATH
728
\c IMPORT_PATH can be used to add to the search paths where other QML modules
729
that this one depends on can be found. The other modules must have their
730
\c qmldir file under their own target path below one of the search paths.
731
732
If the backing target is a static library and that static library will be
733
installed, \c OUTPUT_TARGETS should be given to provide a variable in which to
734
store a list of additional targets that will also need to be installed.
735
These additional targets are generated internally by \c{qt_add_qml_module()}
736
and are referenced by the backing target's linking requirements as part of
737
ensuring that resources are set up and loaded correctly.
738
739
\target PLUGIN_TARGET
740
\section2 Targets and plugin targets
741
742
\c PLUGIN_TARGET specifies the plugin target associated with the QML module.
743
The \c PLUGIN_TARGET can be the same as the backing
744
\c target, in which case there will be no separate backing target.
745
If \c PLUGIN_TARGET is not given, it defaults to \c target with \c plugin
746
appended. For example, a backing target called \c mymodule would have a default
747
plugin name of \c mymoduleplugin. The plugin target's name will be used to
748
populate a \c{plugin} line in the generated
749
\l{Module Definition qmldir Files}{qmldir} file. Therefore, you must not try to
750
change the plugin's output name by setting target properties like
751
\c OUTPUT_NAME or any of its related properties.
752
753
The backing \c target and the plugin target (if different) will be created by
754
the command, unless they already exist. Projects should generally let them be
755
created by the command so that they are created as the appropriate target type.
756
If the backing \c target is a static library, the plugin will also be created
757
as a static library. If the backing \c target is a shared library, the plugin
758
will be created as a module library. If an existing \c target is passed in and
759
it is an executable target, there will be no plugin. If you intend to always
760
link directly to the backing target and do not need a plugin, it can be
761
disabled by adding the \c NO_PLUGIN option. Specifying both \c NO_PLUGIN and
762
\c PLUGIN_TARGET is an error.
763
764
\target NO_CREATE_PLUGIN_TARGET
765
In certain situations, the project may want to delay creating the plugin target
766
until after the call. The \c NO_CREATE_PLUGIN_TARGET option can be given in
767
that situation. The project is then expected to call
768
\l{qt6_add_qml_plugin}{qt_add_qml_plugin()} on the plugin target once it has
769
been created. When \c NO_CREATE_PLUGIN_TARGET is given, \c PLUGIN_TARGET must
770
also be provided to explicitly name the plugin target.
771
772
\target CLASS_NAME
773
\target NO_GENERATE_PLUGIN_SOURCE
774
By default, \c{qt_add_qml_module()} will auto-generate a \c{.cpp} file that
775
implements the plugin class named by the \c CLASS_NAME argument. The generated
776
\c{.cpp} file will be automatically added to the plugin target as a source file
777
to be compiled. If the project wants to provide its own implementation of the
778
plugin class, the \c NO_GENERATE_PLUGIN_SOURCE option should be given. Where no
779
\c CLASS_NAME is provided, it defaults to the \c URI with dots replaced by
780
underscores, then \c Plugin appended. Unless the QML module has no plugin, the
781
class name will be recorded as a \c classname line in the generated
782
\l{Module Definition qmldir Files}{qmldir} file. You need to add any C++ files
783
with custom plugin code to the plugin target. Since the plugin then likely
784
contains functionality that goes beyond simply loading the backing library, you
785
will probably want to add \l{NO_PLUGIN_OPTIONAL}, too. Otherwise the QML engine
786
may skip loading the plugin if it detects that the backing library is already
787
linked.
788
789
\target NO_PLUGIN
790
If the \c NO_PLUGIN keyword is given, then no plugin will be built. This
791
keyword is thus incompatible with all the options that customize the plugin
792
target, in particular \l{NO_GENERATE_PLUGIN_SOURCE}, \l{NO_PLUGIN_OPTIONAL},
793
\l{PLUGIN_TARGET}, \l{NO_CREATE_PLUGIN_TARGET}, and \l{CLASS_NAME}. If you do
794
not provide a plugin for your module, it will only be fully usable if its
795
backing library has been linked into the executable. It is generally hard to
796
guarantee that a linker preserves the linkage to a library it considers unused.
797
798
\target NO_PLUGIN_OPTIONAL
799
If the \c NO_PLUGIN_OPTIONAL keyword is given, then the plugin is recorded in
800
the generated \c qmldir file as non-optional. If all of a QML module's
801
functionality is implemented in its backing target and the plugin target is
802
separate, then the plugin can be optional, which is the default and recommended
803
arrangement. The auto-generated plugin source file satisfies this requirement.
804
Where a project provides its own \c{.cpp} implementation for the plugin, that
805
would normally mean the \c NO_PLUGIN_OPTIONAL keyword is also needed because
806
the plugin will almost certainly contain functionality that the QML module
807
requires.
808
809
\section2 Automatic type registration
810
811
Type registration is automatically performed for the backing target's C++
812
sources that are processed by AUTOMOC. This will generate a typeinfo file in the
813
\l{OUTPUT_DIRECTORY}{output directory}, the file name being the \c target name
814
with \c{.qmltypes} appended. This file name can be changed using the
815
\c TYPEINFO option if desired, but this should not normally be necessary.
816
The file name is also recorded as a \c typeinfo entry in the generated
817
\l{Module Definition qmldir Files}{qmldir} file. Automatic type registration
818
can be disabled using the \c NO_GENERATE_QMLTYPES option, in which case no
819
typeinfo file will be generated, but the project will still be expected to
820
generate a typeinfo file and place it in the same directory as the generated
821
\c qmldir file.
822
823
\target OUTPUT_DIRECTORY
824
\c OUTPUT_DIRECTORY specifies where the plugin library, \c qmldir and typeinfo
825
files are generated. When this keyword is not given, the default value will be
826
the target path (formed from the \c URI) appended to the value of the
827
\l QT_QML_OUTPUT_DIRECTORY variable.
828
If that variable is not defined, the default depends on the type of backing
829
target. For executables, the value will be the target path appended to
830
\c{${CMAKE_CURRENT_BINARY_DIR}}, whereas for other targets it will be just
831
\c{${CMAKE_CURRENT_BINARY_DIR}}. When the structure of the source tree
832
matches the structure of QML module target paths (which is highly recommended),
833
\l QT_QML_OUTPUT_DIRECTORY often isn't needed. In order to match the structure
834
of the target paths, you have to call your directories \e exactly like the
835
segments of your module URI. For example, if your module URI is
836
\c{MyUpperCaseThing.mylowercasething}, you need to put this in a directory
837
called \c{MyUpperCaseThing/mylowercasething/}.
838
839
The need for specifying the \c OUTPUT_DIRECTORY keyword should be rare, but if
840
it is used, it is likely that the caller will also need to add to the
841
\l IMPORT_PATH to ensure that \l{qmllint-auto}{linting},
842
\l{qmlcachegen-auto}{cached compilation} of qml sources,
843
\l{qt6_import_qml_plugins}{automatic importing} of plugins in static builds,
844
and \l{qt_deploy_qml_imports}{deploying imported QML modules} for non-static
845
builds all work correctly.
846
847
\section2 Qt Quick Designer compatibility
848
849
\c DESIGNER_SUPPORTED should be given if the QML module supports
850
Qt Quick Designer. When present, the generated \c qmldir file will contain
851
a \c designersupported line. See \l{Module Definition qmldir Files} for how
852
this affects the way Qt Quick Designer handles the plugin.
853
854
\section2 Keeping module versions in sync
855
856
The \c FOLLOW_FOREIGN_VERSIONING keyword relates to base types of your own
857
C++-defined QML types that live in different QML modules. Typically, the
858
versioning scheme of your module does not match that of the module providing
859
the base types. Therefore, by default all revisions of the base types are
860
made available in any import of your module. If \c FOLLOW_FOREIGN_VERSIONING
861
is given, the version information attached to the base types and their
862
properties is respected. So, an \c {import MyModule 2.8} will then only make
863
available versioned properties up to version \c{2.8} of any base types outside
864
\c{MyModule}.
865
This is mostly useful if you want to keep your module version in sync
866
with other modules you're basing types on. In that case you might want your custom
867
types to not expose properties from a module's base type version greater than the one being
868
imported.
869
870
\section2 C++ namespaces of generated code
871
872
If a namespace is given with the \c NAMESPACE keyword, the plugin and registration
873
code will be generated into a C++ namespace of this name.
874
875
\section2 qmlimportscanner and NO_IMPORT_SCAN
876
877
For static Qt builds, \c{qmlimportscanner} is run at configure time to scan the
878
\c{.qml} files of a QML module and identify the QML imports it uses (see
879
\l{qt6_import_qml_plugins}{qt_import_qml_plugins()}). For non-static Qt builds,
880
if the target is an executable, a similar scan is performed at build time to
881
provide the information needed by deployment scripts (see
882
\l{qt6_deploy_qml_imports}{qt_deploy_qml_imports()}). Both scans can be
883
disabled by providing the \c{NO_IMPORT_SCAN} option. Doing so means the project
884
takes on the responsibility of ensuring all required plugins are instantiated
885
and linked for static builds. For non-static builds the project must manually
886
work out and deploy all QML modules used by an executable target.
887
888
\section2 DISCARD_QML_CONTENTS
889
890
\target DISCARD_QML_CONTENTS
891
By default, QML and JS source file contents are included in the target's resource system.
892
Use \c DISCARD_QML_CONTENTS to remove these contents and reduce the binary size.
893
894
\note If you omit the source code from the binary, the QML engine has to
895
rely on the compilation units created by \l{qmlcachegen} or \l{qmlsc}.
896
Those are tied to the specific version of Qt they were built with. If you change
897
the version of Qt your application uses, they can't be loaded anymore.
898
899
\section2 Arguments for qmltc
900
901
\target ENABLE_TYPE_COMPILER
902
\c ENABLE_TYPE_COMPILER can be used to compile \c{.qml} files to C++ source code
903
with \l{QML type compiler}{qmltc}. Files with the source property
904
\c{QT_QML_SKIP_TYPE_COMPILER} are not compiled to C++.
905
906
\c TYPE_COMPILER_NAMESPACE argument allows to override the namespace in which
907
\l{QML type compiler}{qmltc} generates code.
908
By default, the namespace of the generated code follows the module
909
hierarchy as depicted in the URI,
910
e.g., \c MyModule for a module with URI \c MyModule or
911
\c com::example::Module for URI \c com.example.MyModule.
912
By specifying the \c TYPE_COMPILER_NAMESPACE option, the generated code
913
can be put instead in a custom namespace, where different subnamespaces are to
914
be separated by a "::", e.g. "MyNamespace::MySubnamespace" for the namespace MySubnamespace that
915
is inside the MyNamespace. Apart from the "::", C++ namespace naming rules
916
apply.
917
918
\c QMLTC_QMLTC_EXPORT_DIRECTIVE should be used with \c QMLTC_EXPORT_FILE_NAME when
919
the classes generated by \l{QML type compiler}{qmltc} should be exported from
920
the qml library. By default, classes generated by qmltc are not exported from
921
their library.
922
The header defining the export macro for the current library
923
can be specified as an optional argument to \c QMLTC_EXPORT_FILE_NAME while the
924
exporting macro name should be specified as an argument to
925
\c QMLTC_QMLTC_EXPORT_DIRECTIVE. If no additional include is required or wanted,
926
e.g. when the header of the export macro is already indirectly included by a base
927
class, then the \c QMLTC_EXPORT_FILE_NAME option can be left out.
928
*/
qtdeclarative
src
qml
doc
src
cmake
qt_add_qml_module.qdoc
Generated on
for Qt by
1.16.1