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\keyword qmldir
8\brief Defines a QML module.
9
10There 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
16This documentation covers only the second form of \c qmldir file, which
17lists the QML types, JavaScript files, and plugins that are available under a
18module. 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
25qmldir manually only if you need to use \c qmake.
26
27A \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
45In addition to commands, you can also add comments, which are lines starting
46with \c {#}.
47
48\section2 Module Identifier Declaration
49
50\code
51 module <ModuleIdentifier>
52\endcode
53
54Declares the module identifier of the module. The <ModuleIdentifier> is the
55(dotted URI notation) identifier for the module, which must match the module's
56install path.
57
58The \l{Identified Modules#Semantics of Identified Modules}
59{module identifier directive} must be the first line of the file. Exactly one
60module identifier directive may exist in the \c qmldir file.
61
62Example:
63\code
64 module ExampleModule
65\endcode
66
67\section2 Object Type Declaration
68\code
69 [singleton] <TypeName> <InitialVersion> <File>
70\endcode
71
72Declares a \l{qtqml-typesystem-objecttypes.html}{QML object type}
73to 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
83Zero or more object type declarations may exist in the \c qmldir
84file. However, each object type must have a unique type name within
85any particular version of the module.
86\note To declare a \c singleton type, the QML file defining the
87type must include the \c {pragma Singleton} statement.
88
89Example:
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
121Declares an object type that is in the module but should not be
122made available to users of the module.
123
124Zero or more internal object type declarations may exist in the
125\c qmldir file.
126
127Example:
128\code
129 internal MyPrivateType MyPrivateType.qml
130\endcode
131
132This 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
135on a non-exported type within the module, the engine must also
136load the non-exported type.
137
138\section2 JavaScript Resource Declaration
139
140\code
141 <ResourceIdentifier> <InitialVersion> <File>
142\endcode
143
144Declares a JavaScript file to be made available by the module.
145The resource will be made available via the specified identifier
146with the specified version number.
147
148Zero or more JavaScript resource declarations may exist in the
149\c qmldir file. However, each JavaScript resource must have a unique
150identifier within any particular version of the module.
151
152Example:
153\code
154 MyScript 1.0 MyScript.js
155\endcode
156
157See 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
168Declares 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
189By default, the engine searches for the plugin library in the
190directory that contains the \c qmldir file. (The plugin search
191path can be queried with QQmlEngine::pluginPathList() and
192modified using QQmlEngine::addPluginPath().)
193
194Zero or more C++ plugin declarations may exist in the \c qmldir
195file. However, since plugin loading is a relatively expensive
196operation, clients are advised to specify at most a single plugin.
197
198Example:
199\code
200 plugin MyPluginLibrary
201\endcode
202
203\section2 Plugin Classname Declaration
204
205\code
206 classname <C++ plugin class>
207\endcode
208
209Provides the class name of the C++ plugin used by the module.
210
211This information is required for all the QML modules that depend
212on a C++ plugin for additional functionality. Qt Quick applications
213built with static linking cannot resolve the module imports without
214this information.
215
216\section2 Type Description File Declaration
217
218\code
219 typeinfo <File>
220\endcode
221
222Declares a \l{Type Description Files}{type description file} for
223the module that can be read by QML tools such as \l{\QC Documentation}{\QC} to
224access information about the types defined by the module's plugins.
225\c <File> is the (relative) file name of a \c .qmltypes file.
226
227Example:
228\code
229 typeinfo mymodule.qmltypes
230\endcode
231
232Without such a file, QML tools may be unable to offer features such
233as 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
241Declares that this module depends on another.
242
243Example:
244\code
245 depends MyOtherModule 1.0
246\endcode
247
248This declaration is necessary only in cases when the dependency is
249hidden: for example, when the C++ code for one module is used to
250load QML (perhaps conditionally), which then depends on other
251modules. In such cases, the \c depends declaration is necessary
252to include the other modules in application packages.
253
254\section2 Module Import Declaration
255
256\code
257 import <ModuleIdentifier> [<Version>]
258\endcode
259
260Declares that this module imports another.
261
262Example:
263\code
264 import MyOtherModule 1.0
265\endcode
266
267The types from the other module are made available in the same type
268namespace as this module is imported into. Omitting the version
269imports the latest version available of the other module. Specifying
270\c auto as version imports the same version as the version of this
271module specified in the QML \c import statement.
272
273\section2 Designer Support Declaration
274
275\code
276 designersupported
277\endcode
278
279Set this property if the plugin is supported by Qt Quick Designer.
280By default, the plugin will not be supported.
281
282A plugin that is supported by Qt Quick Designer has to be properly
283tested. This means that the plugin does not crash when running inside
284the qml2puppet that is used by Qt Quick Designer to execute QML.
285Generally, the plugin should work well in the Qt Quick Designer
286and not cause any show stoppers, like taking excessive amounts of memory,
287slowing down the qml2puppet heavily, or anything else that renders
288the plugin effectively unusable in the Qt Quick Designer.
289
290The items of an unsupported plugin are not painted in the Qt Quick Designer,
291but 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
299This property directs the QML engine to load any further files for this
300module from <path>, rather than the current directory. This can be used
301to load files compiled with qmlcachegen.
302
303For example, you can add a module's QML files as resources to a resource
304path \c{:/my/path/MyModule/}. Then, add \c{prefer :/my/path/MyModule} to
305the qmldir file in order to use the files in the resource system, rather
306than the ones in the file system. If you then use qmlcachegen for those,
307the pre-compiled files will be available to any clients of the module.
308
309\section1 Versioning Semantics
310
311All QML types that are exported for a particular major version are available
312with the latest version of the same major version. For example, if a module
313provides a \c MyButton type in version 1.0 and \c MyWindow type in version 1.1,
314clients 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
316particular minor version cannot be used by importing an older or earlier minor
317version. 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
321A module can offer multiple major versions but the clients have access
322to one major version only at a time. For example, importing
323\c{MyExampleModule 2.0} provides access to that major version only and not
324the previous major version. Although you can organize the artifacts that belong
325to different major versions under a single directory and a \c qmldir file, it is
326recommended to use different directories for each major version. If you
327choose to go with the earlier approach (one directory and a \c qmldir file),
328try to use the version suffix for the file names. For example, artifacts
329that belong to \c{MyExampleModule 2.0} can use \c .2 suffix in their file name.
330
331A version cannot be imported if no types have been explicitly exported for that
332version. 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
334that module.
335
336A type can be defined by different files in different minor versions. In this case,
337the most closely matching version is used when imported by clients.
338For example, if a module had specified the following types via its \c qmldir
339file:
340
341\code
342module ExampleModule
343MyButton 1.0 MyButton.qml
344MyButton 1.1 MyButton11.qml
345MyButton 1.3 MyButton13.qml
346MyRectangle 1.2 MyRectangle12.qml
347\endcode
348
349a client who imports version \c 1.2 of \c ExampleModule can use the \c MyButton
350type definition provided by \c MyButton11.qml as it is the latest version of that
351type, and the \c MyRectangle type definition provided by \c MyRectangle12.qml.
352
353The version system ensures that a given QML file works regardless of the
354version of installed software, as a versioned import only imports types
355for that version, leaving other identifiers available, even if the actual
356installed version might otherwise provide those identifiers.
357
358\section1 Example of a qmldir File
359
360One example of a \c qmldir file follows:
361
362\code
363module ExampleModule
364CustomButton 2.0 CustomButton20.qml
365CustomButton 2.1 CustomButton21.qml
366plugin examplemodule
367MathFunctions 2.0 mathfuncs.js
368\endcode
369
370The above \c qmldir file defines a module called "ExampleModule". It defines
371the \c CustomButton QML object type in versions 2.0 and 2.1 of the
372module, with different implementations for each version. It specifies a plugin
373that must be loaded by the engine when the module is imported by clients, and
374that plugin may register various C++-defined types with the QML type system.
375On Unix-like systems the QML engine attempts to load \c libexamplemodule.so
376as a QQmlExtensionPlugin, and on Windows it loads \c examplemodule.dll as a
377QQmlExtensionPlugin. Finally, the \c qmldir file specifies a
378\l{qtqml-javascript-resources.html}{JavaScript resource}, which is
379only available if version 2.0 or a later version (under the same major version)
380of the module is imported.
381
382If the module is \l{qtqml-modules-identifiedmodules.html}{installed} into the
383QML import path, clients could import and use the module in the following
384manner:
385
386\qml
387import QtQuick 2.0
388import ExampleModule 2.1
389
390Rectangle {
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
403The \c CustomButton type used above would come from the definition specified in
404the \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
409QML modules may refer to one or more type information files in their
410\c qmldir file. These usually have the \c .qmltypes
411extension and are read by external tools to gain information about
412types defined in C++ and typically imported via plugins.
413
414As such qmltypes files have no effect on the functionality of a QML module.
415Their only use is to allow tools such as \l{\QC Documentation}{\QC} to provide code completion,
416error checking and other functionality to users of your module.
417
418Any module that defines QML types in C++ should also ship a type description
419file.
420
421The best way to create a qmltypes file for your module is to generate it
422using the build system and the \l QML_ELEMENT macros. If you follow the
423documentation on this, no further action is needed. qmltyperegistrar will
424automatically generate the \c .qmltypes files.
425
426Example:
427If your module is in \c /tmp/imports/My/Module, a file called \c plugins.qmltypes
428should be generated alongside the actual plugin binary.
429
430Add the line
431\code
432typeinfo plugins.qmltypes
433\endcode
434to \c /tmp/imports/My/Module/qmldir to register it.
435
436*/