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
qmldir.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-qmldir.html
5
\title Module Definition qmldir Files
6
\brief Defines a QML module
7
8
There are two distinct types of \c qmldir files:
9
\list
10
\li QML document directory listing files
11
\li QML module definition files
12
\endlist
13
14
This documentation covers only the second form of \c qmldir file, which
15
lists the QML types, JavaScript files, and plugins that are available under a
16
module. For more information about the first form of \c qmldir file, see
17
\l{qtqml-syntax-directoryimports.html#directory-listing-qmldir-files}
18
{directory listing qmldir files}.
19
20
\section1 Contents of a Module Definition qmldir File
21
22
\note Use the CMake API to \l{Port QML modules to CMake}{generate qmldir files}. Write your \c
23
qmldir manually only if you need to use \c qmake.
24
25
A \c qmldir file is a plain-text file that contains the following commands:
26
27
\list
28
\li \l {Module Identifier Declaration}
29
\li \l {Object Type Declaration}
30
\li \l {Internal Object Type Declaration}
31
\li \l {JavaScript Resource Declaration}
32
\li \l {Plugin Declaration}
33
\li \l {Plugin Classname Declaration}
34
\li \l {Type Description File Declaration}
35
\li \l {Module Dependencies Declaration}
36
\li \l {Module Import Declaration}
37
\li \l {Designer Support Declaration}
38
\li \l {Preferred Path Declaration}
39
\endlist
40
41
\note Each command in a \c qmldir file must be on a separate line.
42
43
In addition to commands, you can also add comments, which are lines starting
44
with \c {#}.
45
46
\section2 Module Identifier Declaration
47
48
\code
49
module <ModuleIdentifier>
50
\endcode
51
52
Declares the module identifier of the module. The <ModuleIdentifier> is the
53
(dotted URI notation) identifier for the module, which must match the module's
54
install path.
55
56
The \l{Identified Modules#Semantics of Identified Modules}
57
{module identifier directive} must be the first line of the file. Exactly one
58
module identifier directive may exist in the \c qmldir file.
59
60
Example:
61
\code
62
module ExampleModule
63
\endcode
64
65
\section2 Object Type Declaration
66
\code
67
[singleton] <TypeName> <InitialVersion> <File>
68
\endcode
69
70
Declares a \l{qtqml-typesystem-objecttypes.html}{QML object type}
71
to be made available by the module.
72
\list
73
\li \c [singleton] Optional. Used to declare a singleton type.
74
\li \c <TypeName> is the type being made available
75
\li \c <InitialVersion> is the module version for which the type is to be
76
made available
77
\li \c <File> is the (relative) file name of the QML file that defines
78
the type
79
\endlist
80
81
Zero or more object type declarations may exist in the \c qmldir
82
file. However, each object type must have a unique type name within
83
any particular version of the module.
84
\note To declare a \c singleton type, the QML file defining the
85
type must include the \c {pragma Singleton} statement.
86
87
Example:
88
\code
89
//Style.qml with custom singleton type definition
90
pragma Singleton
91
import QtQuick 2.0
92
93
QtObject {
94
property int textSize: 20
95
property color textColor: "green"
96
}
97
98
// qmldir declaring the singleton type
99
module CustomStyles
100
singleton Style 1.0 Style.qml
101
102
// singleton type in use
103
import QtQuick 2.0
104
import CustomStyles 1.0
105
106
Text {
107
font.pixelSize: Style.textSize
108
color: Style.textColor
109
text: "Hello World"
110
}
111
\endcode
112
113
\section2 Internal Object Type Declaration
114
115
\code
116
internal <TypeName> <File>
117
\endcode
118
119
Declares an object type that is in the module but should not be
120
made available to users of the module.
121
122
Zero or more internal object type declarations may exist in the
123
\c qmldir file.
124
125
Example:
126
\code
127
internal MyPrivateType MyPrivateType.qml
128
\endcode
129
130
This is necessary if the module is imported remotely
131
(see \l{Identified Modules#Remotely Installed Identified Modules}
132
{Remotely Installed Identified Modules}) because if an exported type depends
133
on a non-exported type within the module, the engine must also
134
load the non-exported type.
135
136
\section2 JavaScript Resource Declaration
137
138
\code
139
<ResourceIdentifier> <InitialVersion> <File>
140
\endcode
141
142
Declares a JavaScript file to be made available by the module.
143
The resource will be made available via the specified identifier
144
with the specified version number.
145
146
Zero or more JavaScript resource declarations may exist in the
147
\c qmldir file. However, each JavaScript resource must have a unique
148
identifier within any particular version of the module.
149
150
Example:
151
\code
152
MyScript 1.0 MyScript.js
153
\endcode
154
155
See the documentation about \l{qtqml-javascript-resources.html}
156
{defining JavaScript resources} and
157
\l{qtqml-javascript-imports.html}
158
{Importing JavaScript Resources In QML} for more information.
159
160
\section2 Plugin Declaration
161
162
\code
163
[optional] plugin <Name> [<Path>]
164
\endcode
165
166
Declares a plugin to be made available by the module.
167
168
\list
169
\li \c optional denotes that the plugin itself does not contain
170
any relevant code and only serves to load a library it links
171
to. If given, and if any types for the module are already
172
available, indicating that the library has been loaded by some
173
other means, QML will not load the plugin.
174
\li \c <Name> is the plugin library name. This is usually not the
175
same as the file name of the plugin binary, which is platform
176
dependent. For example, the library \c MyAppTypes would produce
177
\c libMyAppTypes.so on Linux and \c MyAppTypes.dll on Windows.
178
\li \c <Path> (optional) specifies either:
179
\list
180
\li an absolute path to the directory containing the plugin
181
file, or
182
\li a relative path from the directory containing the \c qmldir
183
file to the directory containing the plugin file.
184
\endlist
185
\endlist
186
187
By default, the engine searches for the plugin library in the
188
directory that contains the \c qmldir file. (The plugin search
189
path can be queried with QQmlEngine::pluginPathList() and
190
modified using QQmlEngine::addPluginPath().)
191
192
Zero or more C++ plugin declarations may exist in the \c qmldir
193
file. However, since plugin loading is a relatively expensive
194
operation, clients are advised to specify at most a single plugin.
195
196
Example:
197
\code
198
plugin MyPluginLibrary
199
\endcode
200
201
\section2 Plugin Classname Declaration
202
203
\code
204
classname <C++ plugin class>
205
\endcode
206
207
Provides the class name of the C++ plugin used by the module.
208
209
This information is required for all the QML modules that depend
210
on a C++ plugin for additional functionality. Qt Quick applications
211
built with static linking cannot resolve the module imports without
212
this information.
213
214
\section2 Type Description File Declaration
215
216
\code
217
typeinfo <File>
218
\endcode
219
220
Declares a \l{Type Description Files}{type description file} for
221
the module that can be read by QML tools such as \l{\QC Documentation}{\QC} to
222
access information about the types defined by the module's plugins.
223
\c <File> is the (relative) file name of a \c .qmltypes file.
224
225
Example:
226
\code
227
typeinfo mymodule.qmltypes
228
\endcode
229
230
Without such a file, QML tools may be unable to offer features such
231
as code completion for the types defined in your plugins.
232
233
\section2 Module Dependencies Declaration
234
235
\code
236
depends <ModuleIdentifier> <InitialVersion>
237
\endcode
238
239
Declares that this module depends on another.
240
241
Example:
242
\code
243
depends MyOtherModule 1.0
244
\endcode
245
246
This declaration is necessary only in cases when the dependency is
247
hidden: for example, when the C++ code for one module is used to
248
load QML (perhaps conditionally), which then depends on other
249
modules. In such cases, the \c depends declaration is necessary
250
to include the other modules in application packages.
251
252
\section2 Module Import Declaration
253
254
\code
255
import <ModuleIdentifier> [<Version>]
256
\endcode
257
258
Declares that this module imports another.
259
260
Example:
261
\code
262
import MyOtherModule 1.0
263
\endcode
264
265
The types from the other module are made available in the same type
266
namespace as this module is imported into. Omitting the version
267
imports the latest version available of the other module. Specifying
268
\c auto as version imports the same version as the version of this
269
module specified in the QML \c import statement.
270
271
\section2 Designer Support Declaration
272
273
\code
274
designersupported
275
\endcode
276
277
Set this property if the plugin is supported by Qt Quick Designer.
278
By default, the plugin will not be supported.
279
280
A plugin that is supported by Qt Quick Designer has to be properly
281
tested. This means that the plugin does not crash when running inside
282
the qml2puppet that is used by Qt Quick Designer to execute QML.
283
Generally, the plugin should work well in the Qt Quick Designer
284
and not cause any show stoppers, like taking excessive amounts of memory,
285
slowing down the qml2puppet heavily, or anything else that renders
286
the plugin effectively unusable in the Qt Quick Designer.
287
288
The items of an unsupported plugin are not painted in the Qt Quick Designer,
289
but they are still available as empty boxes and the properties can be edited.
290
291
\section2 Preferred Path Declaration
292
293
\code
294
prefer <Path>
295
\endcode
296
297
This property directs the QML engine to load any further files for this
298
module from <path>, rather than the current directory. This can be used
299
to load files compiled with qmlcachegen.
300
301
For example, you can add a module's QML files as resources to a resource
302
path \c{:/my/path/MyModule/}. Then, add \c{prefer :/my/path/MyModule} to
303
the qmldir file in order to use the files in the resource system, rather
304
than the ones in the file system. If you then use qmlcachegen for those,
305
the pre-compiled files will be available to any clients of the module.
306
307
\section1 Versioning Semantics
308
309
All QML types that are exported for a particular major version are available
310
with the latest version of the same major version. For example, if a module
311
provides a \c MyButton type in version 1.0 and \c MyWindow type in version 1.1,
312
clients importing version \c 1.1 of the module get to use the \c MyButton and
313
\c MyWindow types. However, the reverse is not true: a type exported for a
314
particular minor version cannot be used by importing an older or earlier minor
315
version. In the example mentioned earlier, if the client had imported version
316
\c 1.0 of the module, they can use the \c MyButton type only but not the
317
\c MyWindow type.
318
319
A module can offer multiple major versions but the clients have access
320
to one major version only at a time. For example, importing
321
\c{MyExampleModule 2.0} provides access to that major version only and not
322
the previous major version. Although you can organize the artifacts that belong
323
to different major versions under a sigle directory and a \c qmldir file, it is
324
recommended to use different directories for each major version. If you
325
choose to go with the earlier approach (one directory and a \c qmldir file),
326
try to use the version suffix for the file names. For example, artifacts
327
that belong to \c{MyExampleModule 2.0} can use \c .2 suffix in their file name.
328
329
A version cannot be imported if no types have been explicitly exported for that
330
version. If a module provides a \c MyButton type in version 1.0 and a
331
\c MyWindow type in version 1.1, you cannot import version 1.2 or version 2.0 of
332
that module.
333
334
A type can be defined by different files in different minor versions. In this case,
335
the most closely matching version is used when imported by clients.
336
For example, if a module had specified the following types via its \c qmldir
337
file:
338
339
\code
340
module ExampleModule
341
MyButton 1.0 MyButton.qml
342
MyButton 1.1 MyButton11.qml
343
MyButton 1.3 MyButton13.qml
344
MyRectangle 1.2 MyRectangle12.qml
345
\endcode
346
347
a client who imports version \c 1.2 of \c ExampleModule can use the \c MyButton
348
type definition provided by \c MyButton11.qml as it is the latest version of that
349
type, and the \c MyRectangle type definition provided by \c MyRectangle12.qml.
350
351
The version system ensures that a given QML file works regardless of the
352
version of installed software, as a versioned import only imports types
353
for that version, leaving other identifiers available, even if the actual
354
installed version might otherwise provide those identifiers.
355
356
\section1 Example of a qmldir File
357
358
One example of a \c qmldir file follows:
359
360
\code
361
module ExampleModule
362
CustomButton 2.0 CustomButton20.qml
363
CustomButton 2.1 CustomButton21.qml
364
plugin examplemodule
365
MathFunctions 2.0 mathfuncs.js
366
\endcode
367
368
The above \c qmldir file defines a module called "ExampleModule". It defines
369
the \c CustomButton QML object type in versions 2.0 and 2.1 of the
370
module, with different implementations for each version. It specifies a plugin
371
that must be loaded by the engine when the module is imported by clients, and
372
that plugin may register various C++-defined types with the QML type system.
373
On Unix-like systems the QML engine attempts to load \c libexamplemodule.so
374
as a QQmlExtensionPlugin, and on Windows it loads \c examplemodule.dll as a
375
QQmlExtensionPlugin. Finally, the \c qmldir file specifies a
376
\l{qtqml-javascript-resources.html}{JavaScript resource}, which is
377
only available if version 2.0 or a later version (under the same major version)
378
of the module is imported.
379
380
If the module is \l{qtqml-modules-identifiedmodules.html}{installed} into the
381
QML import path, clients could import and use the module in the following
382
manner:
383
384
\qml
385
import QtQuick 2.0
386
import ExampleModule 2.1
387
388
Rectangle {
389
width: 400
390
height: 400
391
color: "lightsteelblue"
392
393
CustomButton {
394
color: "gray"
395
text: "Click Me!"
396
onClicked: MathFunctions.generateRandom() > 10 ? color = "red" : color = "gray";
397
}
398
}
399
\endqml
400
401
The \c CustomButton type used above would come from the definition specified in
402
the \c CustomButton21.qml file, and the JavaScript resource identified by the
403
\c MathFunctions identifier would be defined in the \c mathfuncs.js file.
404
405
\section1 Type Description Files
406
407
QML modules may refer to one or more type information files in their
408
\c qmldir file. These usually have the \c .qmltypes
409
extension and are read by external tools to gain information about
410
types defined in C++ and typically imported via plugins.
411
412
As such qmltypes files have no effect on the functionality of a QML module.
413
Their only use is to allow tools such as \l{\QC Documentation}{\QC} to provide code completion,
414
error checking and other functionality to users of your module.
415
416
Any module that defines QML types in C++ should also ship a type description
417
file.
418
419
The best way to create a qmltypes file for your module is to generate it
420
using the build system and the \l QML_ELEMENT macros. If you follow the
421
documentation on this, no further action is needed. qmltyperegistrar will
422
automatically generate the \c .qmltypes files.
423
424
Example:
425
If your module is in \c /tmp/imports/My/Module, a file called \c plugins.qmltypes
426
should be generated alongside the actual plugin binary.
427
428
Add the line
429
\code
430
typeinfo plugins.qmltypes
431
\endcode
432
to \c /tmp/imports/My/Module/qmldir to register it.
433
434
*/
qtdeclarative
src
qml
doc
src
qmllanguageref
modules
qmldir.qdoc
Generated on
for Qt by
1.14.0