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