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_target_qml_sources.qdoc
Go to the documentation of this file.
1// Copyright (C) 2021 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GFDL-1.3-no-invariants-only
3
4/*!
5\page qt-target-qml-sources.html
6\ingroup cmake-commands-qtqml
7
8\title qt_target_qml_sources
9\target qt6_target_qml_sources
10
11\brief Add qml files and resources to an existing QML module target.
12
13\cmakecommandsince 6.2
14
15\section1 Synopsis
16
17\badcode
18qt_target_qml_sources(
19 target
20 [QML_FILES ...]
21 [RESOURCES ...]
22 [PREFIX resource_path]
23 [OUTPUT_TARGETS out_targets_var]
24 [NO_LINT]
25 [NO_CACHEGEN]
26 [NO_QMLDIR_TYPES]
27)
28
29\endcode
30
31\versionlessCMakeCommandsNote qt6_target_qml_sources()
32
33\section1 Description
34
35\note This command requires CMake 3.19 or later.
36
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
48scenarios.
49
50\section1 Arguments
51
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.
54
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.
59
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}.
65
66\c NO_QMLDIR_TYPES prevents the \c QML_FILES from being added as types to the
67generated \l{qmldir-autogeneration}{qmldir} file.
68
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
73way.
74
75\target PREFIX
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.
84
85\badcode
86qt_add_qml_module(backing
87 URI Example
88 VERSION 1.0
89 RESOURCE_PREFIX /my.company.com/imports
90)
91
92qt_target_qml_sources(backing
93 QML_FILES special/First.qml
94 RESOURCES icons/logo.png
95)
96
97qt_target_qml_sources(backing
98 PREFIX /other.company.com/debugging
99 QML_FILES Inspector.qml
100)
101\endcode
102
103In the above example, the \c backing target's resources will end up with the
104following contents:
105
106\list
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}
110\endlist
111
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.
117
118\target qml-source-file-properties
119\section1 Source File Properties
120
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.
127
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.
132
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}).
137
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.
143
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
153version.
154
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}.
162The source properties must be set before \l{qt_add_qml_module}{creating} the
163module the singleton belongs to.
164
165\target QT_RESOURCE_ALIAS
166All files listed with \c QML_FILES or \c RESOURCES will be added to the
167\c{target}'s resources. Their location in the resources consists of a base point
168and a relative path. The base point defaults to the concatenation of the QML
169module's resource prefix and its target path, but these can be overridden with
170the \l PREFIX argument. The relative path will default to the path of the file
171relative to the \c{target}'s \c SOURCE_DIR target property. This relative path
172can be overridden by setting the \c QT_RESOURCE_ALIAS property on the source
173file. This is commonly used to collect files from different directories and
174have them appear in the resources under a common location.
175
176\target qt_target_qml_sources_example
177\snippet cmake/qt_target_qml_sources/CMakeLists.txt 0
178
179In the above example, the \c qt_target_qml_sources_example target's resources
180will end up with the following contents:
181
182\list
183\li \c{/my.company.com/imports/Example/File.qml}
184\li \c{/my.company.com/imports/Example/FunnySingleton.qml}
185\li \c{/my.company.com/imports/Example/templates/File.qml}
186\li \c{/my.company.com/imports/Example/some_old_thing.qml}
187\li \c{/my.company.com/imports/Example/button-types.png}
188\li \c{/my.company.com/imports/Example/doc/README.txt}
189\endlist
190
191The generated \c qmldir file will contain the following type entries:
192
193\badcode
194File 2.0 File.qml
195singleton FunnySingleton 2.0 FunnySingleton.qml
196OldThing 1.1 some_old_thing.qml
197OldThing 2.0 some_old_thing.qml
198\endcode
199
200\note The source FunnySingleton.qml file must already contain
201the \c {pragma Singleton} statement. Setting the \c QT_QML_SINGLETON_TYPE source
202property does not automatically generate the pragma.
203
204\snippet cmake/qt_target_qml_sources/FunnySingleton.qml 0
205
206*/