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
9The \l{qt_add_qml_module} CMake command is the recommended way to define QML
10modules. It automates type registration, resource embedding, plugin creation,
11and tooling integration. This page explains the concepts behind
12\c{qt_add_qml_module} and walks through common usage patterns.
13
14For the full reference of all options, see \l{qt_add_qml_module}.
15
16\section1 Key Concepts
17
18Before looking at examples, it helps to understand what \c{qt_add_qml_module}
19actually 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
42The source directory structure should mirror the module's URI. The URI is
43converted to a path by replacing dots with forward slashes. For example, a
44module with URI \c{MyCompany.Controls} should live in:
45
46\badcode
47src/MyCompany/Controls/CMakeLists.txt
48src/MyCompany/Controls/Button.qml
49src/MyCompany/Controls/mywidget.cpp
50src/MyCompany/Controls/mywidget.h
51\endcode
52
53This convention allows the QML engine and tooling to find modules without
54additional configuration. If your directory structure doesn't match the URI,
55you'll need to set \l{OUTPUT_DIRECTORY} so that the build output lands in a
56path matching the URI, and \l{IMPORT_PATH} so that the QML engine and tooling
57can locate the module.
58
59\section1 Common Patterns
60
61\section2 QML-only Application
62
63The simplest case: an executable with only QML files and no C++ types.
64
65\badcode
66cmake_minimum_required(VERSION 3.16)
67project(myapp LANGUAGES CXX)
68
69find_package(Qt6 REQUIRED COMPONENTS Quick)
70qt_standard_project_setup(REQUIRES 6.8)
71
72qt_add_executable(myapp)
73qt_add_qml_module(myapp
74 URI MyApp
75 QML_FILES
76 Main.qml
77 Button.qml
78 RESOURCES
79 images/logo.png
80)
81
82target_link_libraries(myapp PRIVATE Qt6::Quick)
83\endcode
84
85Because the backing target is an executable, no plugin is created. The QML
86files are compiled and embedded as resources. The \c{RESOURCES} keyword adds
87non-QML files (images, fonts, etc.) to the same resource hierarchy.
88
89\section2 Application with C++ Types
90
91An application that exposes C++ types to QML:
92
93\badcode
94qt_add_executable(myapp)
95qt_add_qml_module(myapp
96 URI MyApp
97 QML_FILES
98 Main.qml
99 SOURCES
100 backend.cpp backend.h
101)
102\endcode
103
104The C++ types in \c{backend.h} must be annotated with \l{QML_ELEMENT} (or
105similar macros like \l{QML_NAMED_ELEMENT}) and must use the \l{Q_OBJECT}
106or \l{Q_GADGET} macro. Type registration happens automatically via
107\c{AUTOMOC}. See \l{Registering C++ Types with the QML Type System} for
108a full overview of the available registration macros.
109
110\section2 Reusable Library Module
111
112A QML module packaged as a library that other projects or modules can import:
113
114\badcode
115# In src/MyCompany/Controls/CMakeLists.txt
116qt_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
126This 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
133Using the \c{mycontrols} module from the \l{Reusable Library Module} example
134above:
135
136\badcode
137# In the application's CMakeLists.txt
138qt_add_executable(myapp)
139qt_add_qml_module(myapp
140 URI MyApp
141 QML_FILES Main.qml
142 DEPENDENCIES TARGET mycontrols
143)
144
145target_link_libraries(myapp PRIVATE mycontrols)
146\endcode
147
148The \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
154import MyCompany.Controls
155
156Button { text: "Click me" }
157\endcode
158
159\section2 Module with Singletons
160
161QML files providing singleton types need a source file property set
162\e{before} the \c{qt_add_qml_module} call:
163
164\badcode
165set_source_files_properties(Theme.qml PROPERTIES QT_QML_SINGLETON_TYPE TRUE)
166
167qt_add_qml_module(mymodule
168 URI MyModule
169 QML_FILES
170 Theme.qml
171 Main.qml
172)
173\endcode
174
175The QML file must also contain \c{pragma Singleton}. Both the CMake property
176and the QML pragma are required.
177
178\section2 Module with Custom Plugin
179
180When you need to perform custom initialization (for example, registering an
181image provider), you can provide your own \l{QQmlEngineExtensionPlugin}
182subclass.
183
184\badcode
185# In CMakeLists.txt
186qt_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
198target_sources(mymoduleplugin PRIVATE plugin.cpp)
199\endcode
200
201\badcode
202// In plugin.cpp
203#include <QtQml/QQmlEngineExtensionPlugin>
204#include "myimageprovider.h"
205
206class MyModulePlugin : public QQmlEngineExtensionPlugin
207{
208 Q_OBJECT
209 Q_PLUGIN_METADATA(IID QQmlEngineExtensionInterface_iid)
210public:
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
221generate the default plugin source. \l{qt_add_qml_module}{CLASS_NAME} must
222match the class name in your implementation.
223\l{qt_add_qml_module}{NO_PLUGIN_OPTIONAL} ensures the plugin is always loaded,
224since it contains initialization logic that would be skipped otherwise.
225
226\section1 Adding Further QML Files
227
228For QML files added after the initial \c{qt_add_qml_module} call, use
229\l{qt_target_qml_sources}:
230
231\code
232qt_target_qml_sources(my_qml_module
233 QML_FILES
234 DynamicallyAddedType.qml
235)
236\endcode
237
238This is useful for conditionally including files based on platform or
239configuration.
240
241\section1 Detailed CMake Reference
242
243For complete details on all CMake commands, properties, variables, and policies,
244see \l{CMake Integration for QML}.
245
246\sa {QML Modules}, {qt_add_qml_module}, {Writing QML Modules}
247*/