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