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
topic.qdoc
Go to the documentation of this file.
1
// Copyright (C) 2017 The Qt Company Ltd.
2
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GFDL-1.3-no-invariants-only
3
4
/*!
5
\page qtqml-modules-topic.html
6
\meta {keywords} {qmltopic}
7
\title QML Modules
8
\brief Description of how to write modules for QML
9
10
A QML module provides versioned types and JavaScript resources in a type
11
namespace which may be used by clients who import the module. The types which
12
a module provides may be defined in C++ within a plugin, or in QML documents.
13
Modules make use of the QML versioning system which allows modules to be
14
independently updated.
15
16
Defining of a QML module allows:
17
\list
18
\li The sharing of common QML types within a project - for example, a group of
19
UI components that are used by different windows
20
\li The distribution of QML-based libraries
21
\li The modularization of distinct features, so that applications only load the
22
libraries necessary for their individual needs
23
\li Versioning of types and resources so that the module can be updated safely
24
without breaking client code
25
\endlist
26
27
\section1 Modern QML Modules
28
29
QML Modules are best defined using the CMake API, in particular \l{qt_add_qml_module}.
30
The following sections describe all aspects of creating a modern QML module.
31
32
\section2 Defining Object Types through QML Documents
33
34
\l{Defining Object Types through QML Documents}{QML types can be defined directly in QML documents},
35
which is the simplest way to create reusable components. Each QML file that starts with a capital
36
letter automatically defines a type that can be used by other QML files in the same module.
37
38
\section2 Defining QML Types from C++
39
40
For better performance or access to low-level APIs, types can be
41
\l{Overview - QML and C++ Integration}{defined in C++ and exposed to QML}. C++ types provide full
42
control over properties, methods, and signals while benefiting from static typing and compilation.
43
44
\section2 Tying it All Together with CMake
45
46
The \l{Tying it all together with CMake}{\c{qt_add_qml_module} CMake command} brings together QML
47
files, C++ types, and resources into a cohesive module. It handles type registration, resource
48
compilation, and QML tooling integration automatically, making it the recommended approach for
49
modern QML module development.
50
51
\section2 Modernizing QML Modules
52
53
This \l{Modernizing QML Modules}{guide} helps migrate older CMake-based QML modules to newer
54
versions of Qt. It covers how to adapt to newer CMake policies and avoid outdated techniques.
55
56
\section2 Fine Tuning QML Modules
57
58
Advanced module configuration options in \l{Writing QML Modules}{Writing QML Modules} allow
59
fine-grained control over module behavior. This includes adding multiple QML modules to the same
60
binary, versioning, custom directory layouts, and advanced topics on QML plugins.
61
62
\section1 Old QML Modules
63
64
\note Use the \l{Port QML modules to CMake}{CMake API} to define a QML Module.
65
Define your QML module manually only if you need to use \c qmake.
66
67
A module is defined by a \l{qtqml-modules-qmldir.html}
68
{module definition qmldir file}. Each module has an associated type
69
namespace, which is the module's identifier. A module can provide QML object
70
types (defined either by QML documents or via a C++ plugin) and JavaScript
71
resources, and may be imported by clients.
72
73
To define a module, a developer should gather together the various QML
74
documents, JavaScript resources and C++ plugins which belong in the module
75
into a single directory, and write an appropriate \l{qtqml-modules-qmldir.html}
76
{module definition qmldir file} which should also be placed into the directory.
77
The directory can then be installed into the
78
\l{qtqml-syntax-imports.html#qml-import-path}{QML import path} as a module.
79
80
Note that defining a module is not the only way to share common QML types
81
within a project - a simple \l{Importing QML Document Directories}
82
{QML document directory import} may also be used for this purpose.
83
84
\section2 Supported QML Module Types
85
86
There are two different types of modules supported by QML:
87
\list
88
\li \l{Identified Modules}
89
\li \l{Legacy Modules} (deprecated)
90
\endlist
91
92
Identified modules explicitly define their identifier and are installed into
93
QML import path. Identified modules are more maintainable (due to type
94
versioning) and are provided with type registration guarantees by the QML
95
engine which are not provided to legacy modules. Legacy modules are only
96
supported to allow legacy code to continue to work with the latest version of
97
QML, and should be avoided by clients if possible.
98
99
Clients may import a QML module from within QML documents or JavaScript files.
100
Please see the documentation about
101
\l{qtqml-syntax-imports.html#module-namespace-imports}{importing a QML module}
102
for more information on the topic.
103
104
\section2 Providing Types and Functionality in a C++ Plugin
105
106
An application which has a lot of logic implemented in C++, or which defines
107
types in C++ and exposes them to QML, may wish to implement a QML plugin. A
108
QML extension module developer may wish to implement some types in a C++ plugin
109
(as opposed to defining them via QML documents) to achieve better performance
110
or for greater flexibility.
111
112
Every C++ plugin for QML has an initialization function which is called by
113
the QML engine when it loads the plugin. This initialization function must
114
register any types that the plugin provides, but must not do anything else
115
(for example, instantiating QObjects is not allowed).
116
117
See \l{qtqml-modules-cppplugins.html}{Creating C++ Plugins For QML} for more information.
118
119
\sa {Modernizing QML Modules}, {Port QML modules to CMake}
120
*/
qtdeclarative
src
qml
doc
src
qmllanguageref
modules
topic.qdoc
Generated on
for Qt by
1.16.1