Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
cmake-module-integration.qdoc
Go to the documentation of this file.
1
// Copyright (C) 2026 The Qt Company Ltd.
2
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GFDL-1.3-no-invariants-only
3
4
/*!
5
\page qtqml-modules-cmake-integration.html
6
\title Tying it all together with CMake
7
\brief Building QML modules with CMake
8
9
The \l{qt_add_qml_module} CMake command is the recommended way to define QML
10
modules. It automates type registration, resource embedding, plugin creation,
11
and tooling integration. This page explains the concepts behind
12
\c{qt_add_qml_module} and walks through common usage patterns.
13
14
For the full reference of all options, see \l{qt_add_qml_module}.
15
16
\section1 Key Concepts
17
18
Before looking at examples, it helps to understand what \c{qt_add_qml_module}
19
actually creates:
20
21
\list
22
\li A \e{backing target} — a library (or executable) containing the module's
23
C++ code, compiled QML files, and resources. This is the first argument to
24
\c{qt_add_qml_module} and is the target you link against. It can either
25
be an existing target (created earlier with \c{qt_add_executable} or
26
\c{qt_add_library}) or \c{qt_add_qml_module} will create it for you.
27
\li A \e{plugin target} — a small library that allows the QML engine to load
28
the module dynamically at run time. It is created automatically and is
29
separate from the backing target. When an executable links directly to the
30
backing target, the plugin is not loaded at run time. The plugin can be
31
omitted entirely using \l{qt_add_qml_module}{NO_PLUGIN}.
32
\li A \e{qmldir file} — a module metadata file that tells the QML engine what
33
types the module provides and where to find them. It is generated
34
automatically.
35
\li A \e{typeinfo file} (\c{.qmltypes}) — machine-readable type information
36
used by QML tooling (\l{qmlcachegen}, \l{qmllint}, \l{\QMLLS}{qmlls},
37
Qt Creator). Also generated automatically.
38
\endlist
39
40
\section1 Directory Structure
41
42
The source directory structure should mirror the module's URI. The URI is
43
converted to a path by replacing dots with forward slashes. For example, a
44
module with URI \c{MyCompany.Controls} should live in:
45
46
\badcode
47
src/MyCompany/Controls/CMakeLists.txt
48
src/MyCompany/Controls/Button.qml
49
src/MyCompany/Controls/mywidget.cpp
50
src/MyCompany/Controls/mywidget.h
51
\endcode
52
53
This convention allows the QML engine and tooling to find modules without
54
additional configuration. If your directory structure doesn't match the URI,
55
you'll need to set \l{OUTPUT_DIRECTORY} so that the build output lands in a
56
path matching the URI, and \l{IMPORT_PATH} so that the QML engine and tooling
57
can locate the module.
58
59
\section1 Common Patterns
60
61
\section2 QML-only Application
62
63
The simplest case: an executable with only QML files and no C++ types.
64
65
\badcode
66
cmake_minimum_required(VERSION 3.16)
67
project(myapp LANGUAGES CXX)
68
69
find_package(Qt6 REQUIRED COMPONENTS Quick)
70
qt_standard_project_setup(REQUIRES 6.8)
71
72
qt_add_executable(myapp)
73
qt_add_qml_module(myapp
74
URI MyApp
75
QML_FILES
76
Main.qml
77
Button.qml
78
RESOURCES
79
images/logo.png
80
)
81
82
target_link_libraries(myapp PRIVATE Qt6::Quick)
83
\endcode
84
85
Because the backing target is an executable, no plugin is created. The QML
86
files are compiled and embedded as resources. The \c{RESOURCES} keyword adds
87
non-QML files (images, fonts, etc.) to the same resource hierarchy.
88
89
\section2 Application with C++ Types
90
91
An application that exposes C++ types to QML:
92
93
\badcode
94
qt_add_executable(myapp)
95
qt_add_qml_module(myapp
96
URI MyApp
97
QML_FILES
98
Main.qml
99
SOURCES
100
backend.cpp backend.h
101
)
102
\endcode
103
104
The C++ types in \c{backend.h} must be annotated with \l{QML_ELEMENT} (or
105
similar macros like \l{QML_NAMED_ELEMENT}) and must use the \l{Q_OBJECT}
106
or \l{Q_GADGET} macro. Type registration happens automatically via
107
\c{AUTOMOC}. See \l{Registering C++ Types with the QML Type System} for
108
a full overview of the available registration macros.
109
110
\section2 Reusable Library Module
111
112
A QML module packaged as a library that other projects or modules can import:
113
114
\badcode
115
# In src/MyCompany/Controls/CMakeLists.txt
116
qt_add_qml_module(mycontrols
117
URI MyCompany.Controls
118
QML_FILES
119
Button.qml
120
Slider.qml
121
SOURCES
122
theme.cpp theme.h
123
)
124
\endcode
125
126
This creates two targets: \c{mycontrols} (the backing library) and
127
\c{mycontrolsplugin} (the plugin). Other modules import it with
128
\c{import MyCompany.Controls} in QML. Applications that link to
129
\c{mycontrols} directly don't need to load the plugin at run time.
130
131
\section2 Application Using a Library Module
132
133
Using the \c{mycontrols} module from the \l{Reusable Library Module} example
134
above:
135
136
\badcode
137
# In the application's CMakeLists.txt
138
qt_add_executable(myapp)
139
qt_add_qml_module(myapp
140
URI MyApp
141
QML_FILES Main.qml
142
DEPENDENCIES TARGET mycontrols
143
)
144
145
target_link_libraries(myapp PRIVATE mycontrols)
146
\endcode
147
148
The \l{qt_add_qml_module}{DEPENDENCIES} line ensures that QML tooling
149
(\l{qmlcachegen}, \l{qmllint}, \l{\QMLLS}{qmlls}) can find the types provided by
150
\c{MyCompany.Controls}.
151
152
\badcode
153
// In Main.qml
154
import MyCompany.Controls
155
156
Button { text: "Click me" }
157
\endcode
158
159
\section2 Module with Singletons
160
161
QML files providing singleton types need a source file property set
162
\e{before} the \c{qt_add_qml_module} call:
163
164
\badcode
165
set_source_files_properties(Theme.qml PROPERTIES QT_QML_SINGLETON_TYPE TRUE)
166
167
qt_add_qml_module(mymodule
168
URI MyModule
169
QML_FILES
170
Theme.qml
171
Main.qml
172
)
173
\endcode
174
175
The QML file must also contain \c{pragma Singleton}. Both the CMake property
176
and the QML pragma are required.
177
178
\section2 Module with Custom Plugin
179
180
When you need to perform custom initialization (for example, registering an
181
image provider), you can provide your own \l{QQmlEngineExtensionPlugin}
182
subclass.
183
184
\badcode
185
# In CMakeLists.txt
186
qt_add_qml_module(mymodule
187
URI MyModule
188
NO_GENERATE_PLUGIN_SOURCE
189
NO_PLUGIN_OPTIONAL
190
CLASS_NAME MyModulePlugin
191
QML_FILES
192
Main.qml
193
SOURCES
194
myimageprovider.cpp myimageprovider.h
195
)
196
197
# Add the custom plugin source to the plugin target
198
target_sources(mymoduleplugin PRIVATE plugin.cpp)
199
\endcode
200
201
\badcode
202
// In plugin.cpp
203
#include <QtQml/QQmlEngineExtensionPlugin>
204
#include "myimageprovider.h"
205
206
class MyModulePlugin : public QQmlEngineExtensionPlugin
207
{
208
Q_OBJECT
209
Q_PLUGIN_METADATA(IID QQmlEngineExtensionInterface_iid)
210
public:
211
void initializeEngine(QQmlEngine *engine, const char *uri) override
212
{
213
engine->addImageProvider("myprovider", new MyImageProvider);
214
}
215
};
216
217
#include "plugin.moc"
218
\endcode
219
220
\l{qt_add_qml_module}{NO_GENERATE_PLUGIN_SOURCE} tells the build system not to
221
generate the default plugin source. \l{qt_add_qml_module}{CLASS_NAME} must
222
match the class name in your implementation.
223
\l{qt_add_qml_module}{NO_PLUGIN_OPTIONAL} ensures the plugin is always loaded,
224
since it contains initialization logic that would be skipped otherwise.
225
226
\section1 Adding Further QML Files
227
228
For QML files added after the initial \c{qt_add_qml_module} call, use
229
\l{qt_target_qml_sources}:
230
231
\code
232
qt_target_qml_sources(my_qml_module
233
QML_FILES
234
DynamicallyAddedType.qml
235
)
236
\endcode
237
238
This is useful for conditionally including files based on platform or
239
configuration.
240
241
\section1 Detailed CMake Reference
242
243
For complete details on all CMake commands, properties, variables, and policies,
244
see \l{CMake Integration for QML}.
245
246
\sa {QML Modules}, {qt_add_qml_module}, {Writing QML Modules}
247
*/
qtdeclarative
src
qml
doc
src
qmllanguageref
modules
cmake-module-integration.qdoc
Generated on
for Qt by
1.16.1