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