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
identifiedmodules.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
\page qtqml-modules-identifiedmodules.html
5
\meta {keywords} {qmltopic}
6
\title Identified Modules
7
\brief Creating and importing identified modules
8
9
Identified modules are modules that are installed and identifiable to the QML
10
engine by a URI in the form of a dotted identifier string, which should be
11
specified by the module in its \c qmldir file. This enables such modules to
12
be imported with a unique identifier that remains the same no matter where the
13
module is located on the local file system.
14
15
When importing an identified module, an unquoted identifier is used, with an
16
optional version number:
17
18
\snippet qml/imports/installed-module.qml imports
19
20
Identified modules must be installed into the
21
\l{qtqml-syntax-imports.html#qml-import-path}{import path} in order to be found
22
by the QML engine.
23
24
Syntactically, each dot-separated segment of the URI must be a well-formed
25
ECMAScript Identifier Name. This means, for example, the segments must not start
26
with a number and they must not contain \e{-} (minus) characters. As the URI
27
will be translated into directory names, you should restrict it to alphanumeric
28
characters of the latin alphabet, underscores, and dots.
29
30
\section1 Locally Installed Identified Modules
31
32
A directory of QML and/or C++ files can be shared as an identified module if it
33
contains a \l{qtqml-modules-qmldir.html}{qmldir file} with the module metadata
34
and is installed into the QML import path. Any QML file on the local file
35
system can import this directory as a module by using an
36
\l{qtqml-syntax-imports.html}{import} statement that refers to the module's
37
URI, enabling the file to use the \l{qtqml-typesystem-objecttypes.html}
38
{QML object types} and \l{qtqml-javascript-resources.html}
39
{JavaScript resources} defined by the module.
40
41
The module's \c qmldir file must reside in a directory structure within the
42
\l{qtqml-syntax-imports.html#qml-import-path}{import path} that reflects the
43
URI dotted identifier string, where each dot (".") in the identifier reflects
44
a sub-level in the directory tree. For example, the \c qmldir file of the
45
module \c com.mycompany.mymodule must be located in the sub-path
46
\c com/mycompany/mymodule/qmldir somewhere in the
47
\l{qtqml-syntax-imports.html#qml-import-path}{import path}.
48
49
It is possible to store different versions of a module in subdirectories of its
50
own. For example, a version 2.1 of a module could be located under
51
\c com/mycompany/mymodule.2/qmldir or \c com/mycompany/mymodule.2.1/qmldir.
52
The engine will automatically load the module which matches best.
53
54
Alternatively, versioning for different types can be defined within a qmldir
55
file itself, however this can make updating such a module more difficult (as a
56
\c qmldir file merge must take place as part of the update procedure).
57
58
59
\section2 An Example
60
61
Consider the following QML project directory structure. Under the top level
62
directory \c myapp, there are a set of common UI components in a sub-directory
63
named \c mycomponents, and the main application code in a sub-directory named
64
\c main, like this:
65
66
\code
67
myapp
68
|- mycomponents
69
|- CheckBox.qml
70
|- DialogBox.qml
71
|- Slider.qml
72
|- main
73
|- application.qml
74
\endcode
75
76
To make the \c mycomponents directory available as an identified module, the
77
directory must include a \l{qtqml-modules-qmldir.html}{qmldir file} that
78
defines the module identifier, and describes the object types made available by
79
the module. For example, to make the \c CheckBox, \c DialogBox and \c Slider
80
types available for version 1.0 of the module, the \c qmldir file would contain
81
the following:
82
83
\code
84
module myapp.mycomponents
85
CheckBox 1.0 CheckBox.qml
86
DialogBox 1.0 DialogBox.qml
87
Slider 1.0 Slider.qml
88
\endcode
89
90
Additionally, the location of the \c qmldir file in the
91
\l{qtqml-syntax-imports.html#qml-import-path}{import path} must match the
92
module's dotted identifier string. So, say the top level \c myapp directory is
93
located in \c C:\qml\projects, and say the module should be identified as
94
"myapp.mycomponents". In this case:
95
96
\list
97
\li The path \c C:\qml\projects should be added to the
98
\l{qtqml-syntax-imports.html#qml-import-path}{import path}
99
\li The qmldir file should be located under \c C:\qml\projects\myapp\mycomponents\qmldir
100
\endlist
101
102
Once this is done, a QML file located anywhere on the local filesystem can
103
import the module by referring to its URI and the appropriate version:
104
105
\qml
106
import myapp.mycomponents 1.0
107
108
DialogBox {
109
CheckBox {
110
// ...
111
}
112
Slider {
113
// ...
114
}
115
}
116
\endqml
117
118
\section1 Remotely Installed Identified Modules
119
120
Identified modules are also accessible as a network resource. In the previous
121
example, if the \c C:\qml\projects directory was hosted as
122
\c http://www.some-server.com/qml/projects and this URL was added to the QML
123
import path, the module could be imported in exactly the same way.
124
125
Note that when a file imports a module over a network, it can only access QML
126
and JavaScript resources provided by the module; it cannot access any types
127
defined by C++ plugins in the module.
128
129
130
\section1 Semantics of Identified Modules
131
132
An identified module is provided with the following guarantees by the QML
133
engine:
134
\list
135
\li other modules are unable to modify or override types in the module's
136
namespace
137
\li other modules are unable to register new types into the module's
138
namespace
139
\li usage of type names by clients will resolve deterministically to a given
140
type definition depending on the versioning specified and the import order
141
\endlist
142
143
This ensures that clients which use the module can be certain that the
144
object types defined in the module will behave as the module author documented.
145
146
An identified module has several restrictions upon it:
147
\list
148
\li an identified module must be installed into the
149
\l{qtqml-syntax-imports.html#qml-import-path}{QML import path}
150
\li the module identifier specified in the \l{qtqml-modules-qmldir.html}
151
{module identifier directive} must match the install path of the module
152
(relative to the QML import path, where directory separators are replaced
153
with period characters)
154
\li the module must register its types into the module identifier type
155
namespace
156
\li the module may not register types into any other module's namespace
157
\endlist
158
159
For example, if an identified module is installed into
160
\c{$QML_IMPORT_PATH/ExampleModule}, the module identifier directive must be:
161
\code
162
module ExampleModule
163
\endcode
164
165
If the strict module is installed into
166
\c{$QML_IMPORT_PATH/com/example/CustomUi}, the module identifier directive
167
must be:
168
\code
169
module com.example.CustomUi
170
\endcode
171
172
Clients will then be able to import the above module with the following import
173
statement:
174
\qml
175
import com.example.CustomUi
176
\endqml
177
178
\sa {Modernizing QML Modules}, {Port QML modules to CMake}
179
*/
qtdeclarative
src
qml
doc
src
qmllanguageref
modules
identifiedmodules.qdoc
Generated on
for Qt by
1.16.1