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
qtqml-writing-a-module.qdoc
Go to the documentation of this file.
1// Copyright (C) 2020 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GFDL-1.3-no-invariants-only
3
4/*!
5\page qtqml-writing-a-module.html
6\title Writing QML Modules
7\brief How to write a custom QML module.
8
9You should declare a QML module using the \l{qt_add_qml_module}
10{CMake QML Module API} to:
11
12\list
13\li Generate \l {Module Definition qmldir Files}{qmldir} and
14 \l {Type Description Files}{*.qmltypes files}.
15\li Register C++ types annotated with \l QML_ELEMENT.
16\li Combine QML files and C++-based types in the same module.
17\li Invoke \l {Ahead-of-Time-Compilation}{qmlcachegen} on all
18 QML files.
19\li Use the pre-compiled versions of QML files inside the module.
20\li Provide the module both in the physical and in the
21 \l{The Qt Resource System}{resource file system}.
22\li Create a backing library and an optional plugin. Link the backing library
23 into the application to avoid loading the plugin at run time.
24\endlist
25
26All the above actions can also be configured separately.
27For more information, see \l {qt_add_qml_module}{CMake QML Module API}.
28
29\section1 Multiple QML Modules in One Binary
30
31You can add multiple QML modules into the same binary. Define a CMake target for
32each module and then link the targets to the executable.
33If the extra targets are all static libraries, the result will be one binary,
34which contains multiple QML modules. In short you can create an application
35like this:
36
37\badcode
38myProject
39 | - CMakeLists.txt
40 | - main.cpp
41 | - main.qml
42 | - onething.h
43 | - onething.cpp
44 | - ExtraModule
45 | - CMakeLists.txt
46 | - Extra.qml
47 | - extrathing.h
48 | - extrathing.cpp
49\endcode
50
51To begin, let's assume main.qml contains an instantiation of Extra.qml:
52
53 \badcode
54 import ExtraModule
55 Extra { ... }
56 \endcode
57
58The extra module has to be a static library so that you can link it
59into the main program. Therefore, state as much in ExtraModule/CMakeLists.txt:
60
61\quotefromfile qml/CMakeLists.txt
62\printuntil extrathing.h
63\printuntil )
64
65This generates two targets: \c extra_module for the backing library, and
66\c extra_moduleplugin for the plugin. Being a static library too, the plugin cannot
67be loaded at runtime.
68
69In myProject/CMakeLists.txt you need to specify the QML module that main.qml
70and any types declared in onething.h are part of:
71
72\quotefromfile qml/myProject-CMakeLists.txt
73\printuntil onething.h
74\printuntil )
75
76
77From there, you add the subdirectory for the extra module:
78
79\quotefromfile qml/CMakeLists.txt
80\skipto add_subdirectory
81\printuntil )
82
83To ensure that linking the extra module works correctly, you need to:
84
85\list
86\li Define a symbol in the extra module.
87\li Create a reference to the symbol from the main program.
88\endlist
89
90QML plugins contain a symbol you can use for this purpose.
91You can use the \l Q_IMPORT_QML_PLUGIN macro to create a reference to this symbol.
92Add the following code to the main.cpp:
93
94\badcode
95#include <QtQml/QQmlExtensionPlugin>
96Q_IMPORT_QML_PLUGIN(ExtraModulePlugin)
97\endcode
98
99\c ExtraModulePlugin is the name of the generated plugin class. It's composed
100of the module URI with \c Plugin appended to it. Then, in the main program's
101CMakeLists.txt, link the plugin, not the backing library, into the main program:
102
103\quotefromfile qml/myProject-CMakeLists.txt
104\skipto target_link_libraries
105\printuntil )
106
107\section1 Versions
108
109QML has a complex system to assign versions to components and modules. In most
110cases you should ignore all of it by:
111
112\list 1
113 \li Never adding a version to your import statements
114 \li Never specifying any versions in \l{qt_add_qml_module}
115 \li Never using \l{QML_ADDED_IN_VERSION} or \l{QT_QML_SOURCE_VERSIONS}
116 \li Never using \l{Q_REVISION} or the \c{REVISION()} attribute in \l{Q_PROPERTY}
117 \li Avoiding unqualified access
118 \li Generously using import namespaces
119\endlist
120
121Versioning is ideally handled outside the language itself. You may, for example,
122keep separate \l{QML Import Path}{import paths} for different sets of QML modules.
123Or you may use a versioning mechanism provided by your operating system to install
124or uninstall packages with QML modules.
125
126In some cases, Qt's own QML modules may show different behavior, depending on what
127version is imported. In particular, if a property is added to a QML component, and
128your code contains unqualified access to another property of the same name, your
129code will break. In the following example, the code will behave differently
130depending on the version of Qt, because the \l [QML] {Rectangle::}{topLeftRadius}
131property was added in Qt 6.7:
132
133\qml
134import QtQuick
135
136Item {
137 // property you want to use
138 property real topLeftRadius: 24
139
140 Rectangle {
141
142 // correct for Qt version < 6.7 but uses Rectangle's topLeftRadius in 6.7
143 objectName: "top left radius:" + topLeftRadius
144 }
145}
146\endqml
147
148The solution here is to avoid the unqualified access. \l{qmllint Reference}{qmllint} can be used to
149find such problems. The following example accesses the property you actually mean
150in a safe, qualified way:
151
152\qml
153import QtQuick
154
155Item {
156 id: root
157
158 // property you want to use
159 property real topLeftRadius: 24
160
161 Rectangle {
162
163 // never mixes up topLeftRadius with unrelated Rectangle's topLeftRadius
164 objectName: "top left radius:" + root.topLeftRadius
165 }
166}
167\endqml
168
169You can also avoid the incompatibility by importing a specific version of QtQuick:
170
171\qml
172// make sure Rectangle has no topLeftRadius property
173import QtQuick 6.6
174
175Item {
176 property real topLeftRadius: 24
177 Rectangle {
178 objectName: "top left radius:" + topLeftRadius
179 }
180}
181\endqml
182
183Another problem solved by versioning is the fact that QML components imported by
184different modules may shadow each other. In the following example, if \c{MyModule} were
185to introduce a component named \c{Rectangle} in a newer version, the \c{Rectangle}
186created by this document would not be a \c {QQuickRectangle} anymore, but rather the
187new \c{Rectangle} introduced by \c{MyModule}.
188
189\qml
190import QtQuick
191import MyModule
192
193Rectangle {
194 // MyModule's Rectangle, not QtQuick's
195}
196\endqml
197
198A good way to avoid the shadowing would be to import \c{QtQuick} and/or \c{MyModule}
199into type namespaces as follows:
200
201\qml
202import QtQuick as QQ
203import MyModule as MM
204
205QQ.Rectangle {
206 // QtQuick's Rectangle
207}
208\endqml
209
210Alternatively, if you import \c{MyModule} with a fixed version, and the new component
211receives a correct version tag via \l{QML_ADDED_IN_VERSION} or \l{QT_QML_SOURCE_VERSIONS},
212the shadowing is also avoided:
213
214\qml
215import QtQuick 6.6
216
217// Types introduced after 1.0 are not available, like Rectangle for example
218import MyModule 1.0
219
220Rectangle {
221 // QtQuick's Rectangle
222}
223\endqml
224
225For this to work, you need to use versions in \c{MyModule}. There are a few things
226to be aware of.
227
228\section2 If you add versions, add them everywhere
229
230You need to add a \c{VERSION} attribute to \l{qt_add_qml_module}. The version should
231be the most recent version provided by your module. Older minor versions of the same
232major version will automatically be registered. For older major versions, see
233\l{#Exporting Multiple Major Versions from The Same Module}{below}.
234
235You should add \l{QML_ADDED_IN_VERSION} or \l{QT_QML_SOURCE_VERSIONS} to every type
236that was \e not introduced in version \c{x.0} of your module, where \c{x} is the current
237major version.
238
239If you forget to add a version tag, the component will be available in all versions,
240making the versioning ineffective.
241
242However, there is no way to add versions to properties, methods, and signals defined
243in QML. The only way to version QML documents is to add a new document with separate
244\l{QT_QML_SOURCE_VERSIONS} for each change.
245
246\section2 Versions are not transitive
247
248If a component from your module \c{A} imports another module \c{B} and instantiates a type
249from that module as the root element, then the import version of \c{B} is relevant for the
250properties available from the resulting component, no matter what version of \c{A} is
251imported by a user.
252
253Consider a file \c{TypeFromA.qml} with version \c{2.6} in module \c{A}:
254
255\qml
256import B 2.7
257
258// Exposes TypeFromB 2.7, no matter what version of A is imported
259TypeFromB { }
260\endqml
261
262Now consider a user of \c{TypeFromA}:
263
264\qml
265import A 2.6
266
267// This is TypeFromB 2.7.
268TypeFromA { }
269\endqml
270
271The user hopes to see version \c{2.6} but actually gets version
272\c{2.7} of the base class \c{TypeFromB}.
273
274Therefore, in order to be safe, you not only have to duplicate your QML files and
275give them new versions when you add properties yourself, but also when you bump
276versions of modules you import.
277
278\section2 Qualified access does not honor versioning
279
280Versioning only affects unqualified access to members of a type or the type itself.
281In the example with \c{topLeftRadius}, if you write \c{this.topLeftRadius}, the
282property will be resolved if you're using Qt 6.7, even if you \c{import QtQuick 6.6}.
283
284\section2 Versions and revisions
285
286With \l{QML_ADDED_IN_VERSION}, and the two-argument variants of \l{Q_REVISION} and
287\l{Q_PROPERTY}'s \c{REVISION()}, you can only declare versions that are tightly coupled
288to the \l{QMetaObject}{metaobject's} revision as exposed in \l{QMetaMethod::revision}
289and \l{QMetaProperty::revision}. This means all the types in your type hierarchy have
290to follow the same versioning scheme. This includes any types provided by Qt itself
291that you inherit from.
292
293With \l{QQmlEngine::}{qmlRegisterType} and related functions you can register any mapping
294between metaobject revisions and type versions. You then need to use the one-argument forms
295of \l{Q_REVISION} and the \c{REVISION} attribute of \l{Q_PROPERTY}. However, this
296can become rather complex and confusing and is not recommended.
297
298\section2 Exporting multiple major versions from the same module
299
300\l qt_add_qml_module by default considers the major version given in its
301VERSION argument, even if the individual types declare other versions in their
302added specific version via \l QT_QML_SOURCE_VERSIONS or \l Q_REVISION.
303If a module is available under more than one version, you also need to decide
304what versions the individual QML files are available under. To declare further
305major versions, you can use the \c PAST_MAJOR_VERSIONS option to
306\c qt_add_qml_module as well as the \c {QT_QML_SOURCE_VERSIONS} property on
307individual QML files.
308
309\quotefile qml/MajorProject-CMakeLists.txt
310
311\c MyModule is available in major versions 1, 2, and 3. The maximum version
312available is 3.2. You can import any version 1.x or 2.x with a positive x. For
313Thing.qml and OtherThing.qml we have added explicit version information.
314Thing.qml is available from version 1.4, and OtherThing.qml is available from
315version 2.2. You have to specify the later versions, too, in each
316\c set_source_files_properties() because you may remove QML files
317from a module when bumping the major version. There is no explicit version
318information for OneMoreThing.qml. This means that OneMoreThing.qml is available
319in all major versions, from minor version 0.
320
321With this setup, the generated registration code will register the module
322\c versions using \l qmlRegisterModule() for each of the major versions. This
323way, all versions can be imported.
324
325
326\section1 Custom Directory Layouts
327
328The easiest way to structure QML modules is to keep them in directories named by
329their URIs. For example, a module My.Extra.Module would live in a directory
330My/Extra/Module relative to the application that uses it. This way, they can
331easily be found at run time and by any tools.
332
333In more complex projects, this convention can be too limiting. You might for
334instance want to group all QML modules in one place to avoid polluting the
335project's root directory. Or you want to reuse a single module in multiple
336applications. For those cases, \c QT_QML_OUTPUT_DIRECTORY in combination with
337\c RESOURCE_PREFIX and \l IMPORT_PATH can be used.
338
339To collect QML modules into a specific output directory, for example a
340subdirectory "qml" in the build directory \l QT_QML_OUTPUT_DIRECTORY, set the
341following in the top-level CMakeLists.txt:
342
343\badcode
344set(QT_QML_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/qml)
345\endcode
346
347The output directories of QML modules move to the new location.
348Likewise, the \c qmllint and \c qmlcachegen invocations are automatically
349adapted to use the new output directory as an \l[QtQml]{QML Import Path}{import path}.
350Because the new output directory is not part of the default QML import path,
351you have to add it explicitly at run time, so that the QML modules can be found.
352
353
354Now that the physical file system is taken care of, you may still want to move
355the QML modules into a different place in the resource file system. This is what
356the RESOURCE_PREFIX option is for. You have to specify it separately in
357each \l qt_add_qml_module. The QML module will then be placed under the specified
358prefix, with a target path generated from the URI appended. For example,
359consider the following module:
360
361\code
362qt_add_qml_module(
363 URI My.Great.Module
364 VERSION 1.0
365 RESOURCE_PREFIX /example.com/qml
366 QML_FILES
367 A.qml
368 B.qml
369)
370\endcode
371
372This will add a directory \c example.com/qml/My/Great/Module to the resource file
373system and place the QML module defined above in it. You don't strictly need to
374add the resource prefix to the QML import path as the module can still be found
375in the physical file system. However, it generally is a good idea to add the
376resource prefix to the QML import path because loading from the resource file
377system is faster than loading from the physical file system for most modules.
378
379If the QML modules are meant to be used in a larger project with multiple import
380paths, you'll have to do an additional step: Even if you add the import paths at
381run time, tooling like \c qmllint does not have access to it, and might fail to
382find the correct dependencies. Use \c IMPORT_PATH to tell tooling about the
383additional paths it has to consider. For example:
384
385\badcode
386qt_add_qml_module(
387 URI My.Dependent.Module
388 VERSION 1.0
389 QML_FILES
390 C.qml
391 IMPORT_PATH "/some/where/else"
392)
393\endcode
394
395\section1 Eliminating Run Time File System Access
396
397If all QML modules are always loaded from the resource
398file system, you can deploy the application as a single binary.
399
400If \l{QTP0001} policy is set to \c NEW, the \c RESOURCE_PREFIX argument
401for \c{qt_add_qml_module()} defaults to \c{/qt/qml/}, therefore your
402modules are placed in \c{:/qt/qml/} in the resource file system.
403This is part of the default \l{QML Import Path}, but not used by Qt
404itself. For modules to be used within your application, this is the right place.
405
406If you have instead specified a custom \c RESOURCE_PREFIX, you have to add the
407custom resource prefix to the \l{QML Import Path}. You can also add multiple
408resource prefixes:
409
410\badcode
411QQmlEngine qmlEngine;
412qmlEngine.addImportPath(QStringLiteral(":/my/resource/prefix"));
413qmlEngine.addImportPath(QStringLiteral(":/other/resource/prefix"));
414// Use qmlEngine to load the main.qml file.
415\endcode
416
417This might be necessary when using third party libraries to avoid module name
418conflicts. Using a custom resource prefix is discouraged in all other cases.
419
420The path \c :/qt-project.org/imports/ is also part of the default \l{QML Import
421Path}. For modules that are heavily re-used across different projects or Qt
422versions, \c :/qt-project.org/imports/ is acceptable as resource prefix. Qt's
423own QML modules are placed there, though. You have to be careful not to
424overwrite them.
425
426Do not add any unnecessary import paths. The QML engine might find your modules
427in the wrong place then. This can trigger problems which can only be reproduced
428in specific environments.
429
430\section1 Integrating custom QML plugins
431
432If you bundle an \l {QQuickImageProvider}{image provider} in the QML module, you
433need to implement the \l {QQmlEngineExtensionPlugin::initializeEngine()}
434method. This, in turn, makes it necessary to write your own plugin. To support
435this use case, \l NO_GENERATE_PLUGIN_SOURCE can be used.
436
437Let's consider a module that provides its own plugin source:
438
439\quotefile qml/myimageprovider.txt
440
441You may declare an image provider in myimageprovider.h, like this:
442
443\badcode
444class MyImageProvider : public QQuickImageProvider
445{
446 [...]
447};
448\endcode
449
450In plugin.cpp you can then define the QQmlEngineExtensionPlugin:
451
452\quotefile qml/plugin.cpp.txt
453
454This will make the image provider available. The plugin and the backing library
455both are in the same CMake target imageproviderplugin. This is done so that the
456linker does not drop parts of the module in various scenarios.
457
458As the plugin creates an image provider, it no longer has a trivial
459\c initializeEngine function. Therefore, the plugin is no longer optional.
460
461*/