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