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-target-qml-sources.html
6\ingroup cmake-commands-qtqml
8\title qt_target_qml_sources
9\target qt6_target_qml_sources
11\brief Add qml files and resources to an existing QML module target.
22 [PREFIX resource_path]
23 [OUTPUT_TARGETS out_targets_var]
31\versionlessCMakeCommandsNote qt6_target_qml_sources()
35\note This command requires CMake 3.19 or later.
37\c{qt_target_qml_sources()} provides the ability to add more files to a QML
38module after \l{qt6_add_qml_module}{qt_add_qml_module()} has been called.
39Typically, you pass the set of \c{.qml} files and resources to
40\l{qt6_add_qml_module}{qt_add_qml_module()} directly, but in some cases, it may
41be desirable, or even necessary, to add files after
42\l{qt6_add_qml_module}{qt_add_qml_module()} has been called. For example, you
43may wish to add files conditionally based on an \c{if} statement expression,
44or from subdirectories that will only be added if certain criteria are met.
45You might want to add a set of files with different characteristics to the
46others, such as a different resource prefix, or with linting and bytecode
47compilation disabled. The \c{qt_target_qml_sources()} command enables these
52The \c target must be the backing target of a QML module, or if the QML module
53has no separate backing target, it must be the module's plugin target.
55\c QML_FILES is a list of \c{.qml}, \c{.js} and \c{.mjs} files to be added to
56the QML module. This option has exactly the same effect as the \c QML_FILES
57option of the \l{qt6_add_qml_module}{qt_add_qml_module()} command, including
58the automatic compilation to bytecode and lint processing.
60The \c NO_CACHEGEN and \c NO_LINT options also have the same effect as they do
61for \l{qt6_add_qml_module}{qt_add_qml_module()}. They disable the bytecode
62compilation and lint processing for the files listed with \c QML_FILES. This
63behavior can also be specified just for individual files using
64\l{qml-source-file-properties}{source file properties}.
66\c NO_QMLDIR_TYPES prevents the \c QML_FILES from being added as types to the
67generated \l{qmldir-autogeneration}{qmldir} file.
69\c RESOURCES has exactly the same effect as the \c RESOURCES option of the
70\l{qt6_add_qml_module}{qt_add_qml_module()} command. It provides a list of
71files to be added to the \c target as ordinary resources. These files are
72typically things like images, shaders, etc. that the QML code refers to in some
76Files added to the module via \c QML_FILES or \c RESOURCES will be placed under
77the same resource prefix and target path as they would if they were added by the
78\l{qt6_add_qml_module}{qt_add_qml_module()} command. This can be overridden by
79providing a different location with the \c PREFIX option. The value following
80the \c PREFIX keyword will be used directly, without appending any target path.
81The final resource path of each file will be the prefix, plus the path of the
82file below the \c CMAKE_CURRENT_SOURCE_DIR. The \l{QT_RESOURCE_ALIAS} source
83file property can also be used to override that relative path.
86qt_add_qml_module(backing
89 RESOURCE_PREFIX /my.company.com/imports
92qt_target_qml_sources(backing
93 QML_FILES special/First.qml
94 RESOURCES icons/logo.png
97qt_target_qml_sources(backing
98 PREFIX /other.company.com/debugging
99 QML_FILES Inspector.qml
103In the above example, the \c backing target's resources will end up with the
107\li \c{/my.company.com/imports/Example/special/First.qml}
108\li \c{/my.company.com/imports/Example/icons/logo.png}
109\li \c{/other.company.com/debugging/Inspector.qml}
112\c OUTPUT_TARGETS is also analogous to the same option for
113\l{qt6_add_qml_module}{qt_add_qml_module()}. Use it to specify the name of a
114variable in which to store any additional targets created for static builds.
115If the \c target will be installed, these additional targets will also need to
116be installed to satisfy linking requirements.
118\target qml-source-file-properties
119\section1 Source File Properties
121A number of source file properties can be used to influence how each individual
122\c{.qml} file is treated at various points in the QML module processing. These
123override any higher level options specified in calls to
124\c{qt_target_qml_sources()} or \l{qt6_add_qml_module}{qt_add_qml_module()}.
125All of these properties need to be set before the files are added with either
126of those two commands.
128\c QT_QML_SKIP_QMLLINT can be set to \c TRUE on a source file to prevent it
129from being included in the \l{qmllint-auto}{automatic qmllint processing}.
130By default, all \c{.qml} files will be included in the target's lint run, but
131this option can be used to exclude specific files.
133\c QT_QML_SKIP_CACHEGEN does a similar thing, preventing a source file from
134being compiled to byte code when this property is set to \c TRUE. Note that the
135file will still be added to the \c target as a resource in uncompiled form
136(see \l{qmlcachegen-auto}{Caching compiled QML sources}).
138Set the \c QT_QML_SKIP_QMLDIR_ENTRY source file property to \c TRUE to prevent
139that \c{.qml} file from being added as a type to the QML module's typeinfo file
140(see \l{qmldir-autogeneration}{Auto-generating \c{qmldir} and typeinfo files}).
141This would normally only be used for a file that does not expose a public type,
142such as a private JS file.
144By default, when \l{qmldir-autogeneration}{generating the \c qmldir file}, a
145single type entry will be generated for each \c{.qml} file that provides a type.
146It will be given a version number \c{X.0} where \c{X} is the major version of
147the QML module. If the QML module has any \c PAST_MAJOR_VERSIONS set, the same
148pattern will be applied to those too, appending \c{X.0} for each past major
149version \c{X}. For situations where a file needs to provide type entries for
150a different set of versions instead (e.g. it was first added in a minor patch
151version after the \c{.0} release), specify those versions in the source file's
152\c QT_QML_SOURCE_VERSIONS property. One type entry will be created for each
155If the type that a \c{.qml} file provides is a singleton, set its
156\c QT_QML_SINGLETON_TYPE property to \c TRUE. Similarly, the file's
157\c QT_QML_INTERNAL_TYPE source property can be set to \c TRUE to indicate that
158the type it provides is an internal one. The name of the type itself can also
159be overridden using the \c QT_QML_SOURCE_TYPENAME property. All three of these
160will be reflected in the file's type entries in the
161\l{qmldir-autogeneration}{generated \c qmldir file}.
163\target QT_RESOURCE_ALIAS
164All files listed with \c QML_FILES or \c RESOURCES will be added to the
165\c{target}'s resources. Their location in the resources consists of a base point
166and a relative path. The base point defaults to the concatenation of the QML
167module's resource prefix and its target path, but these can be overridden with
168the \l PREFIX argument. The relative path will default to the path of the file
169relative to the \c{target}'s \c SOURCE_DIR target property. This relative path
170can be overridden by setting the \c QT_RESOURCE_ALIAS property on the source
171file. This is commonly used to collect files from different directories and
172have them appear in the resources under a common location.
174\target qt_target_qml_sources_example
175\snippet cmake/qt_target_qml_sources/CMakeLists.txt 0
177In the above example, the \c qt_target_qml_sources_example target's resources
178will end up with the following contents:
181\li \c{/my.company.com/imports/Example/File.qml}
182\li \c{/my.company.com/imports/Example/FunnySingleton.qml}
183\li \c{/my.company.com/imports/Example/templates/File.qml}
184\li \c{/my.company.com/imports/Example/some_old_thing.qml}
185\li \c{/my.company.com/imports/Example/button-types.png}
186\li \c{/my.company.com/imports/Example/doc/README.txt}
189The generated \c qmldir file will contain the following type entries:
193singleton FunnySingleton 2.0 FunnySingleton.qml
194OldThing 1.1 some_old_thing.qml
195OldThing 2.0 some_old_thing.qml
198\note The source FunnySingleton.qml file must already contain
199the \c {pragma Singleton} statement. Setting the \c QT_QML_SINGLETON_TYPE source
200property does not automatically generate the pragma.
202\snippet cmake/qt_target_qml_sources/FunnySingleton.qml 0