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