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