437b41b03796520b351a28e16653744d2c639815
[profile/ivi/qtdeclarative.git] / doc / src / declarative / modules.qdoc
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: http://www.qt-project.org/
6 **
7 ** This file is part of the documentation of the Qt Toolkit.
8 **
9 ** $QT_BEGIN_LICENSE:FDL$
10 ** GNU Free Documentation License
11 ** Alternatively, this file may be used under the terms of the GNU Free
12 ** Documentation License version 1.3 as published by the Free Software
13 ** Foundation and appearing in the file included in the packaging of
14 ** this file.
15 **
16 ** Other Usage
17 ** Alternatively, this file may be used in accordance with the terms
18 ** and conditions contained in a signed written agreement between you
19 ** and Nokia.
20 **
21 **
22 **
23 **
24 ** $QT_END_LICENSE$
25 **
26 ****************************************************************************/
27
28 /*!
29 \page qdeclarativemodules.html
30 \inqmlmodule QtQuick 2
31 \title Modules
32 \section1 QML Modules
33
34
35 A module is a set of QML content files that can be imported as a unit into a QML
36 application. Modules can be used to organize QML content into independent units,
37 and they can use a versioning mechanism that allows for independent
38 upgradability of the modules.
39
40 While QML component files within the same directory are automatically accessible
41 within the global namespace, components defined elsewhere must be imported
42 explicitly using the \c import statement to import them as modules. For
43 example, an \c import statement is required to use:
44
45 \list
46 \o A component defined in another QML file that is not in the same directory
47 \o A component defined in a QML file located on a remote server
48 \o A \l{QDeclarativeExtensionPlugin}{QML extension plugin} library (unless the plugin is installed in the same directory)
49 \o A JavaScript file (note this must be imported using \l {#namespaces}{named imports})
50 \endlist
51
52 An \c import statement includes the module name, and possibly a version number.
53 This can be seen in the snippet commonly found at the top of QML files:
54
55 \snippet doc/src/snippets/declarative/imports/qtquick-1.0.qml import
56
57 This imports version 1.0 of the "QtQuick" module into the global namespace. (The QML
58 library itself must be imported to use any of the \l {QML Elements}, as they
59 are not included in the global namespace by default.)
60
61 The \c Qt module is an \i installed module; it is found in the
62 \l{#import-path}{import path}. There are two types of QML modules:
63 located modules (defined by a URL) and installed modules (defined by a URI).
64
65
66 \section1 Located Modules
67
68 Located modules can reside on the local filesystem or a network resource,
69 and are referred to by a quoted location URL that specifies the filesystem
70 or network URL. They allow any directory with QML content to be imported
71 as a module, whether the directory is on the local filesystem or a remote
72 server.
73
74 For example, a QML project may have a separate directory for a set of
75 custom UI components. These components can be accessed by importing the
76 directory using a relative or absolute path, like this:
77
78 \table
79 \row
80 \o Directory structure
81 \o Contents of application.qml
82
83 \row
84 \o
85 \code
86 MyQMLProject
87     |- MyComponents
88         |- CheckBox.qml
89         |- Slider.qml
90         |- Window.qml
91     |- Main
92         |- application.qml
93 \endcode
94
95 \o
96 \qml
97 import "../MyComponents"
98
99 Window {
100     Slider {
101         // ...
102     }
103     CheckBox {
104         // ...
105     }
106 }
107 \endqml
108
109 \endtable
110
111 Similarly, if the directory resided on a network source, it could
112 be imported like this:
113
114 \snippet doc/src/snippets/declarative/imports/network-imports.qml imports
115
116 A located module can also be imported as a network resource if it has a
117 \l{Writing a qmldir file}{qmldir file} in the directory that specifies the QML files
118 to be made available by the module. For example, if the \c MyComponents directory
119 contained a \c qmldir file defined like this:
120
121 \code
122 Slider 1.0 Slider.qml
123 CheckBox 1.0 CheckBox.qml
124 Window 1.0 Window.qml
125 \endcode
126
127 If the \c MyComponents directory was then hosted as a network resource, it could
128 be imported as a module, like this:
129
130 \qml
131 import "http://the-server-name.com/MyQMLProject/MyComponents"
132
133 Window {
134     Slider {
135         // ...
136     }
137     CheckBox {
138         // ...
139     }
140 }
141 \endqml
142
143 with an optional "1.0" version specification. Notice the import would fail if
144 a later version was used, as the \c qmldir file specifies that these elements
145 are only available in the 1.0 version.
146
147 Note that modules imported as a network resource allow only access to components
148 defined in QML files; components defined by C++ \l{QDeclarativeExtensionPlugin}{QML extension plugins}
149 are not available.
150
151
152 \target import-path
153 \section1 Installed Modules
154
155 Installed modules are modules that are made available through the QML import path,
156 as defined by QDeclarativeEngine::importPathList(), or modules defined within
157 C++ application code. An installed module is referred to by a URI, which allows
158 the module to be imported from QML code without specifying a complete filesystem
159 path or network resource URL.
160
161 When importing an installed module, an un-quoted URI is
162 used, with a mandatory version number:
163
164 \snippet doc/src/snippets/declarative/imports/installed-module.qml imports
165
166 When a module is imported, the QML engine searches the QML import path for a matching
167 module. The root directory of the module must contain a
168 \l{Writing a qmldir file}{qmldir file} that defines the QML files
169 and/or C++ QML extension plugins that are made available to the module.
170
171 Modules that are installed into the import path translate the URI into
172 directory names. For example, the qmldir file of the module \c com.nokia.qml.mymodule
173 must be located in the subpath \c com/nokia/qml/mymodule/qmldir somewhere in the
174 QML import path. In addition it is possible to store different versions of the
175 module in subdirectories of its own. For example, a version 2.1 of the
176 module could be located under \c com/nokia/qml/mymodule.2/qmldir or
177 \c com/nokia/qml/mymodule.2.1/qmldir. The engine will automatically load
178 the module which matches best.
179
180 The import path, as returned by QDeclarativeEngine::importPathList(), defines the default
181 locations to be searched by the QML engine for a matching module. By default, this list
182 contains:
183
184 \list
185 \o The directory of the current file
186 \o The location specified by QLibraryInfo::ImportsPath
187 \o Paths specified by the \c QML_IMPORT_PATH environment variable
188 \endlist
189
190 Additional import paths can be added through QDeclarativeEngine::addImportPath() or the
191 \c QML_IMPORT_PATH environment variable. When running the \l {QML Viewer}, you
192 can also use the \c -I option to add an import path.
193
194
195 \section2 Creating Installed Modules
196
197 As an example, suppose the \c MyQMLProject directory in the \l{Located Modules}{previous example}
198 was located on the local filesystem at \c C:\qml\projects\MyQMLProject. The \c MyComponents
199 subdirectory could be made available as an installed module by adding a
200 \l{Writing a qmldir file}{qmldir file} to the \c MyComponents directory that looked like this:
201
202 \code
203 Slider 1.0 Slider.qml
204 CheckBox 1.0 CheckBox.qml
205 Window 1.0 Window.qml
206 \endcode
207
208 Providing the path \c C:\qml is added to the QML import path using any of the methods listed previously,
209 a QML file located anywhere on the local filesystem can then import the module as shown below,
210 without referring to the module's absolute filesystem location:
211
212 \qml
213 import projects.MyQMLProject.MyComponents 1.0
214
215 Window {
216     Slider {
217         // ...
218     }
219     CheckBox {
220         // ...
221     }
222 }
223 \endqml
224
225 Installed modules are also accessible as a network resource. If the \c C:\qml directory was hosted
226 as \c http://www.some-server.com/qml and this URL was added to the QML import path, the above
227 QML code would work just the same.
228
229 Note that modules imported as a network resource allow only access to components
230 defined in QML files; components defined by C++ \l{QDeclarativeExtensionPlugin}{QML extension plugins}
231 are not available.
232
233
234 \section2 Creating Installed Modules in C++
235
236 C++ applications can define installed modules directly within the application using qmlRegisterType().
237 For example, the \l {Tutorial: Writing QML extensions with C++}{Writing QML extensions with C++ tutorial}
238 defines a C++ class named \c PieChart and makes this type available to QML by calling qmlRegisterType():
239
240 \code
241 qmlRegisterType<PieChart>("Charts", 1, 0, "PieChart");
242 \endcode
243
244 This allows the application's QML files to use the \c PieChart type by importing the declared
245 \c Charts module:
246
247 \snippet doc/src/snippets/declarative/imports/chart.qml import
248
249 For \l{QDeclarativeExtensionPlugin}{QML plugins}, the
250 module URI is automatically passed to QDeclarativeExtensionPlugin::registerTypes(). This method
251 can be reimplemented by the developer to register the necessary types for the module. Below is the
252 \c registerTypes() implementation from the \l{declarative/cppextensions/plugins}{QML plugins}
253 example:
254
255 \snippet examples/declarative/cppextensions/plugins/plugin.cpp plugin
256
257 Once the plugin is built and installed, and includes a \l{Writing a qmldir file}{qmldir file},
258 the module can be imported from QML, like this:
259
260 \snippet doc/src/snippets/declarative/imports/timeexample.qml import
261
262 Unlike QML types defined by QML files, a QML type defined in a C++ extension plugin cannot be loaded by
263 a module that is imported as a network resource.
264
265
266
267 \target namespaces
268 \section1 Namespaces: Using Named Imports
269
270 By default, when a module is imported, its contents are imported into the global namespace. You may choose to import the module into another namespace, either to allow identically-named types to be referenced, or purely for readability.
271
272 To import a module into a specific namespace, use the \i as keyword:
273
274 \snippet doc/src/snippets/declarative/imports/named-imports.qml imports
275
276 Types from these modules can then only be used when qualified by the namespace:
277
278 \snippet doc/src/snippets/declarative/imports/named-imports.qml imported items
279
280 Multiple modules can be imported into the same namespace in the same way that multiple modules can be imported into the global namespace:
281
282 \snippet doc/src/snippets/declarative/imports/merged-named-imports.qml imports
283
284 \section2 JavaScript Files
285
286 JavaScript files must always be imported with a named import:
287
288 \qml
289 import "somescript.js" as MyScript
290
291 Item {
292     //...
293     Component.onCompleted: MyScript.doSomething()
294 }
295 \endqml
296
297 The qualifier ("MyScript" in the above example) must be unique within the QML document.
298 Unlike ordinary modules, multiple scripts cannot be imported into the same namespace.
299
300 Javascript files can be provided by modules, by adding Namespace definitions to the
301 \l{Writing a qmldir file}{qmldir file} for the module.  For example:
302
303 \code
304 SystemFunctions 1.0 SystemFunctions.js
305 UserFunctions 1.0 UserFunctions.js
306 \endcode
307
308 Javascript can be imported from a module, where they will have the namespace defined
309 for them in the module's \c qmldir file:
310
311 \qml
312 import projects.MyQMLProject.MyFunctions 1.0
313
314 Window {
315     Component.onCompleted: { SystemFunctions.cleanUp(); }
316 }
317 \endqml
318
319 Javascript provided by modules can also be imported into namespaces:
320
321 \qml
322 import projects.MyQMLProject.MyFunctions 1.0 as MyFuncs
323 import org.example.Functions 1.0 as TheirFuncs
324
325 Window {
326     Component.onCompleted: {
327         MyFuncs.SystemFunctions.cleanUp();
328         TheirFuncs.SystemFunctions.shutdown();
329     }
330 }
331 \endqml
332
333 \section1 Writing a qmldir File
334
335 A \c qmldir file is a metadata file for a module that maps all type names in
336 the module to versioned QML files. It is required for installed modules, and
337 located modules that are loaded from a network source.
338
339 It is defined by a plain text file named "qmldir" that contains one or more lines of the form:
340
341 \code
342 # <Comment>
343 <TypeName> [<InitialVersion>] <File>
344 internal <TypeName> <File>
345 <Namespace> <InitialVersion> <File>
346 plugin <Name> [<Path>]
347 typeinfo <File>
348 \endcode
349
350 \bold {# <Comment>} lines are used for comments. They are ignored by the QML engine.
351
352 \bold {<TypeName> [<InitialVersion>] <File>} lines are used to add QML files as types.
353 <TypeName> is the type being made available, the optional <InitialVersion> is a version
354 number, and <File> is the (relative) file name of the QML file defining the type.
355
356 Installed files do not need to import the module of which they are a part, as they can refer
357 to the other QML files in the module as relative (local) files, but
358 if the module is imported from a remote location, those files must nevertheless be listed in
359 the \c qmldir file. Types which you do not wish to export to users of your module
360 may be marked with the \c internal keyword: \bold {internal <TypeName> <File>}.
361
362 The same type can be provided by different files in different versions, in which
363 case later versions (e.g. 1.2) must precede earlier versions (e.g. 1.0),
364 since the \i first name-version match is used and a request for a version of a type
365 can be fulfilled by one defined in an earlier version of the module. If a user attempts
366 to import a version earlier than the earliest provided or later than the latest provided,
367 the import produces a runtime error, but if the user imports a version within the range of versions provided,
368 even if no type is specific to that version, no error will occur.
369
370 A single module, in all versions, may only be provided in a single directory (and a single \c qmldir file).
371 If multiple are provided, only the first in the search path will be used (regardless of whether other versions
372 are provided by directories later in the search path).
373
374 The versioning system ensures that a given QML file will work regardless of the version
375 of installed software, since a versioned import \i only imports types for that version,
376 leaving other identifiers available, even if the actual installed version might otherwise
377 provide those identifiers.
378
379 \bold {<Namespace> <InitialVersion> <File>} lines are used to import javascript files
380 into a Namespace exported by the module.  The contents of the script file are made
381 available inside the namespace <Namespace>, which has the version number
382 <InitialVersion>.
383
384 \bold {plugin <Name> [<Path>]} lines are used to add \l{QDeclarativeExtensionPlugin}{QML C++ plugins} to the module. <Name> is the name of the library.  It is usually not the same as the file name
385 of the plugin binary, which is platform dependent; e.g. the library \c MyAppTypes would produce
386 \c libMyAppTypes.so on Linux and \c MyAppTypes.dll on Windows.
387
388 <Path> is an optional argument specifying either an absolute path to the directory containing the
389 plugin file, or a relative path from the directory containing the \c qmldir file to the directory
390 containing the plugin file. By default the engine searches for the plugin library in the directory that contains the \c qmldir
391 file. The plugin search path can be queried with QDeclarativeEngine::pluginPathList() and modified using QDeclarativeEngine::addPluginPath(). When running the \l {QML Viewer}, use the \c -P option to add paths to the plugin search path.
392
393 \bold {typeinfo <File>} lines add \l{Writing a qmltypes file}{type description files} to
394 the module that can be read by QML tools such as Qt Creator to get information about the
395 types defined by the module's plugins. <File> is the (relative) file name of a .qmltypes
396 file.
397
398 Without such a file QML tools may be unable to offer features such as code completion
399 for the types defined in your plugins.
400
401 \section1 Debugging
402
403 The \c QML_IMPORT_TRACE environment variable can be useful for debugging
404 when there are problems with finding and loading modules. See
405 \l{Debugging module imports} for more information.
406
407
408 \section1 Writing a qmltypes file
409
410 QML modules may refer to one or more type information files in their
411 \l{Writing a qmldir file}{qmldir} file. These usually have the .qmltypes
412 extension and are read by external tools to gain information about
413 types defined in plugins.
414
415 As such qmltypes files have no effect on the functionality of a QML module.
416 Their only use is to allow tools such as Qt Creator to provide code completion,
417 error checking and other functionality to users of your module.
418
419 Any module that uses plugins should also ship a type description file.
420
421 The best way to create a qmltypes file for your module is to generate it
422 using the \c qmlplugindump tool that is provided with Qt.
423
424 Example:
425 If your module is in \c /tmp/imports/My/Module, you could run
426 \code
427 qmlplugindump My.Module 1.0 /tmp/imports > /tmp/imports/My/Module/mymodule.qmltypes
428 \endcode
429 to generate type information for your module. Afterwards, add the line
430 \code
431 typeinfo mymodule.qmltypes
432 \endcode
433 to \c /tmp/imports/My/Module/qmldir to register it.
434
435 While the qmldump tool covers most cases, it does not work if:
436 \list
437 \o The plugin uses a \l{QDeclarativeCustomParser}. The component that uses
438    the custom parser will not get its members documented.
439 \o The plugin can not be loaded. In particular if you cross-compiled
440    the plugin for a different architecture, qmldump will not be able to
441    load it.
442 \endlist
443
444 In case you have to create a qmltypes file manually or need to adjust
445 an existing one, this is the file format:
446
447 \qml
448 import QtQuick.tooling 1.1
449
450 // There always is a single Module object that contains all
451 // Component objects.
452 Module {
453     // A Component object directly corresponds to a type exported
454     // in a plugin with a call to qmlRegisterType.
455     Component {
456
457         // The name is a unique identifier used to refer to this type.
458         // It is recommended you simply use the C++ type name.
459         name: "QDeclarativeAbstractAnimation"
460
461         // The name of the prototype Component.
462         prototype: "QObject"
463
464         // The name of the default property.
465         defaultProperty: "animations"
466
467         // The name of the type containing attached properties
468         // and methods.
469         attachedType: "QDeclarativeAnimationAttached"
470
471         // The list of exports determines how a type can be imported.
472         // Each string has the format "URI/Name version" and matches the
473         // arguments to qmlRegisterType. Usually types are only exported
474         // once, if at all.
475         // If the "URI/" part of the string is missing that means the
476         // type should be put into the package defined by the URI the
477         // module was imported with.
478         // For example if this module was imported with 'import Foo 4.8'
479         // the Animation object would be found in the package Foo and
480         // QtQuick.
481         exports: [
482             "Animation 4.7",
483             "QtQuick/Animation 1.0"
484         ]
485
486         // The meta object revisions for the exports specified in 'exports'.
487         // Describes with revisioned properties will be visible in an export.
488         // The list must have exactly the same length as the 'exports' list.
489         // For example the 'animations' propery described below will only be
490         // available through the QtQuick/Animation 1.0 export.
491         exportMetaObjectRevisions: [0, 1]
492
493         Property {
494             name: "animations";
495             type: "QDeclarativeAbstractAnimation"
496             // defaults to false, whether this property is read only
497             isReadonly: true
498             // defaults to false, whether the type of this property was a pointer in C++
499             isPointer: true
500             // defaults to false: whether the type actually is a QDeclarativeListProperty<type>
501             isList: true
502             // defaults to 0: the meta object revision that introduced this property
503             revision: 1
504         }
505         Property { name: "loops"; type: "int" }
506         Property { name: "name"; type: "string" }
507         Property { name: "loopsEnum"; type: "Loops" }
508
509         Enum {
510             name: "Loops"
511             values: {
512                 "Infinite": -2,
513                 "OnceOnly": 1
514             }
515         }
516
517         // Signal and Method work the same way. The inner Parameter
518         // declarations also support the isReadonly, isPointer and isList
519         // attributes which mean the same as for Property
520         Method { name: "restart" }
521         Signal { name: "started"; revision: 2 }
522         Signal {
523             name: "runningChanged"
524             Parameter { type: "bool" }
525             Parameter { name: "foo"; type: "bool" }
526         }
527     }
528 }
529 \endqml
530
531 */
532 /