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