Initial import from the monolithic Qt.
[profile/ivi/qtdeclarative.git] / doc / src / declarative / modules.qdoc
1 /****************************************************************************
2 **
3 ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
6 **
7 ** This file is part of the documentation of the Qt Toolkit.
8 **
9 ** $QT_BEGIN_LICENSE:FDL$
10 ** No Commercial Usage
11 ** This file contains pre-release code and may not be distributed.
12 ** You may use this file in accordance with the terms and conditions
13 ** contained in the Technology Preview License Agreement accompanying
14 ** this package.
15 **
16 ** GNU Free Documentation License
17 ** Alternatively, this file may be used under the terms of the GNU Free
18 ** Documentation License version 1.3 as published by the Free Software
19 ** Foundation and appearing in the file included in the packaging of this
20 ** file.
21 **
22 ** If you have questions regarding the use of this file, please contact
23 ** Nokia at qt-info@nokia.com.
24 ** $QT_END_LICENSE$
25 **
26 ****************************************************************************/
27
28 /*!
29 \page qdeclarativemodules.html
30 \title Modules
31 \section1 QML Modules
32
33
34 A module is a set of QML content files that can be imported as a unit into a QML
35 application. Modules can be used to organize QML content into independent units,
36 and they can use a versioning mechanism that allows for independent
37 upgradability of the modules.
38
39 While QML component files within the same directory are automatically accessible
40 within the global namespace, components defined elsewhere must be imported
41 explicitly using the \c import statement to import them as modules. For
42 example, an \c import statement is required to use:
43
44 \list
45 \o A component defined in another QML file that is not in the same directory
46 \o A component defined in a QML file located on a remote server
47 \o A \l{QDeclarativeExtensionPlugin}{QML extension plugin} library (unless the plugin is installed in the same directory)
48 \o A JavaScript file (note this must be imported using \l {#namespaces}{named imports})
49 \endlist
50
51 An \c import statement includes the module name, and possibly a version number.
52 This can be seen in the snippet commonly found at the top of QML files:
53
54 \snippet doc/src/snippets/declarative/imports/qtquick-1.0.qml import
55
56 This imports version 1.0 of the "QtQuick" module into the global namespace. (The QML
57 library itself must be imported to use any of the \l {QML Elements}, as they
58 are not included in the global namespace by default.)
59
60 The \c Qt module is an \e installed module; it is found in the
61 \l{#import-path}{import path}. There are two types of QML modules:
62 located modules (defined by a URL) and installed modules (defined by a URI).
63
64
65 \section1 Located Modules
66
67 Located modules can reside on the local filesystem or a network resource,
68 and are referred to by a quoted location URL that specifies the filesystem
69 or network URL. They allow any directory with QML content to be imported
70 as a module, whether the directory is on the local filesystem or a remote
71 server.
72
73 For example, a QML project may have a separate directory for a set of
74 custom UI components. These components can be accessed by importing the
75 directory using a relative or absolute path, like this:
76
77 \table
78 \row
79 \o Directory structure
80 \o Contents of application.qml
81
82 \row
83 \o
84 \code
85 MyQMLProject
86     |- MyComponents
87         |- CheckBox.qml
88         |- Slider.qml
89         |- Window.qml
90     |- Main
91         |- application.qml
92 \endcode
93
94 \o
95 \qml
96 import "../MyComponents"
97
98 Window {
99     Slider {
100         // ...
101     }
102     CheckBox {
103         // ...
104     }
105 }
106 \endqml
107
108 \endtable
109
110 Similarly, if the directory resided on a network source, it could
111 be imported like this:
112
113 \snippet doc/src/snippets/declarative/imports/network-imports.qml imports
114
115 A located module can also be imported as a network resource if it has a
116 \l{Writing a qmldir file}{qmldir file} in the directory that specifies the QML files
117 to be made available by the module. For example, if the \c MyComponents directory
118 contained a \c qmldir file defined like this:
119
120 \code
121 Slider 1.0 Slider.qml
122 CheckBox 1.0 CheckBox.qml
123 Window 1.0 Window.qml
124 \endcode
125
126 If the \c MyComponents directory was then hosted as a network resource, it could
127 be imported as a module, like this:
128
129 \qml
130 import "http://the-server-name.com/MyQMLProject/MyComponents"
131
132 Window {
133     Slider {
134         // ...
135     }
136     CheckBox {
137         // ...
138     }
139 }
140 \endqml
141
142 with an optional "1.0" version specification. Notice the import would fail if
143 a later version was used, as the \c qmldir file specifies that these elements
144 are only available in the 1.0 version.
145
146 Note that modules imported as a network resource allow only access to components
147 defined in QML files; components defined by C++ \l{QDeclarativeExtensionPlugin}{QML extension plugins}
148 are not available.
149
150
151 \target import-path
152 \section1 Installed Modules
153
154 Installed modules are modules that are made available through the QML import path,
155 as defined by QDeclarativeEngine::importPathList(), or modules defined within
156 C++ application code. An installed module is referred to by a URI, which allows
157 the module to be imported from QML code without specifying a complete filesystem
158 path or network resource URL.
159
160 When importing an installed module, an un-quoted URI is
161 used, with a mandatory version number:
162
163 \snippet doc/src/snippets/declarative/imports/installed-module.qml imports
164
165 When a module is imported, the QML engine searches the QML import path for a matching
166 module. The root directory of the module must contain a
167 \l{Writing a qmldir file}{qmldir file} that defines the QML files
168 and/or C++ QML extension plugins that are made available to the module.
169
170 Modules that are installed into the import path translate the URI into
171 directory names. For example, the qmldir file of the module \c com.nokia.qml.mymodule
172 must be located in the subpath \c com/nokia/qml/mymodule/qmldir somewhere in the
173 QML import path. In addition it is possible to store different versions of the
174 module in subdirectories of its own. For example, a version 2.1 of the
175 module could be located under \c com/nokia/qml/mymodule.2/qmldir or
176 \c com/nokia/qml/mymodule.2.1/qmldir. The engine will automatically load
177 the module which matches best.
178
179 The import path, as returned by QDeclarativeEngine::importPathList(), defines the default
180 locations to be searched by the QML engine for a matching module. By default, this list
181 contains:
182
183 \list
184 \o The directory of the current file
185 \o The location specified by QLibraryInfo::ImportsPath
186 \o Paths specified by the \c QML_IMPORT_PATH environment variable
187 \endlist
188
189 Additional import paths can be added through QDeclarativeEngine::addImportPath() or the
190 \c QML_IMPORT_PATH environment variable. When running the \l {QML Viewer}, you
191 can also use the \c -I option to add an import path.
192
193
194 \section2 Creating Installed Modules
195
196 As an example, suppose the \c MyQMLProject directory in the \l{Located Modules}{previous example}
197 was located on the local filesystem at \c C:\qml\projects\MyQMLProject. The \c MyComponents
198 subdirectory could be made available as an installed module by adding a
199 \l{Writing a qmldir file}{qmldir file} to the \c MyComponents directory that looked like this:
200
201 \code
202 Slider 1.0 Slider.qml
203 CheckBox 1.0 CheckBox.qml
204 Window 1.0 Window.qml
205 \endcode
206
207 Providing the path \c C:\qml is added to the QML import path using any of the methods listed previously,
208 a QML file located anywhere on the local filesystem can then import the module as shown below,
209 without referring to the module's absolute filesystem location:
210
211 \qml
212 import projects.MyQMLProject.MyComponents 1.0
213
214 Window {
215     Slider {
216         // ...
217     }
218     CheckBox {
219         // ...
220     }
221 }
222 \endqml
223
224 Installed modules are also accessible as a network resource. If the \c C:\qml directory was hosted
225 as \c http://www.some-server.com/qml and this URL was added to the QML import path, the above
226 QML code would work just the same.
227
228 Note that modules imported as a network resource allow only access to components
229 defined in QML files; components defined by C++ \l{QDeclarativeExtensionPlugin}{QML extension plugins}
230 are not available.
231
232
233 \section2 Creating Installed Modules in C++
234
235 C++ applications can define installed modules directly within the application using qmlRegisterType().
236 For example, the \l {Tutorial: Writing QML extensions with C++}{Writing QML extensions with C++ tutorial}
237 defines a C++ class named \c PieChart and makes this type available to QML by calling qmlRegisterType():
238
239 \code
240 qmlRegisterType<PieChart>("Charts", 1, 0, "PieChart");
241 \endcode
242
243 This allows the application's QML files to use the \c PieChart type by importing the declared
244 \c Charts module:
245
246 \snippet doc/src/snippets/declarative/imports/chart.qml import
247
248 For \l{QDeclarativeExtensionPlugin}{QML plugins}, the
249 module URI is automatically passed to QDeclarativeExtensionPlugin::registerTypes(). This method
250 can be reimplemented by the developer to register the necessary types for the module. Below is the
251 \c registerTypes() implementation from the \l{declarative/cppextensions/plugins}{QML plugins}
252 example:
253
254 \snippet examples/declarative/cppextensions/plugins/plugin.cpp plugin
255
256 Once the plugin is built and installed, and includes a \l{Writing a qmldir file}{qmldir file},
257 the module can be imported from QML, like this:
258
259 \snippet doc/src/snippets/declarative/imports/timeexample.qml import
260
261 Unlike QML types defined by QML files, a QML type defined in a C++ extension plugin cannot be loaded by
262 a module that is imported as a network resource.
263
264
265
266 \target namespaces
267 \section1 Namespaces: Using Named Imports
268
269 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.
270
271 To import a module into a specific namespace, use the \e as keyword:
272
273 \snippet doc/src/snippets/declarative/imports/named-imports.qml imports
274
275 Types from these modules can then only be used when qualified by the namespace:
276
277 \snippet doc/src/snippets/declarative/imports/named-imports.qml imported items
278
279 Multiple modules can be imported into the same namespace in the same way that multiple modules can be imported into the global namespace:
280
281 \snippet doc/src/snippets/declarative/imports/merged-named-imports.qml imports
282
283 \section2 JavaScript Files
284
285 JavaScript files must always be imported with a named import:
286
287 \qml
288 import "somescript.js" as MyScript
289
290 Item {
291     //...
292     Component.onCompleted: MyScript.doSomething()
293 }
294 \endqml
295
296 The qualifier ("MyScript" in the above example) must be unique within the QML document.
297 Unlike ordinary modules, multiple scripts cannot be imported into the same namespace.
298
299
300 \section1 Writing a qmldir File
301
302 A \c qmldir file is a metadata file for a module that maps all type names in
303 the module to versioned QML files. It is required for installed modules, and
304 located modules that are loaded from a network source.
305
306 It is defined by a plain text file named "qmldir" that contains one or more lines of the form:
307
308 \code
309 # <Comment>
310 <TypeName> [<InitialVersion>] <File>
311 internal <TypeName> <File>
312 plugin <Name> [<Path>]
313 \endcode
314
315 \bold {# <Comment>} lines are used for comments. They are ignored by the QML engine.
316
317 \bold {<TypeName> [<InitialVersion>] <File>} lines are used to add QML files as types.
318 <TypeName> is the type being made available, the optional <InitialVersion> is a version
319 number, and <File> is the (relative) file name of the QML file defining the type.
320
321 Installed files do not need to import the module of which they are a part, as they can refer
322 to the other QML files in the module as relative (local) files, but
323 if the module is imported from a remote location, those files must nevertheless be listed in
324 the \c qmldir file. Types which you do not wish to export to users of your module
325 may be marked with the \c internal keyword: \bold {internal <TypeName> <File>}.
326
327 The same type can be provided by different files in different versions, in which
328 case later versions (e.g. 1.2) must precede earlier versions (e.g. 1.0),
329 since the \e first name-version match is used and a request for a version of a type
330 can be fulfilled by one defined in an earlier version of the module. If a user attempts
331 to import a version earlier than the earliest provided or later than the latest provided,
332 the import produces a runtime error, but if the user imports a version within the range of versions provided,
333 even if no type is specific to that version, no error will occur.
334
335 A single module, in all versions, may only be provided in a single directory (and a single \c qmldir file).
336 If multiple are provided, only the first in the search path will be used (regardless of whether other versions
337 are provided by directories later in the search path).
338
339 The versioning system ensures that a given QML file will work regardless of the version
340 of installed software, since a versioned import \e only imports types for that version,
341 leaving other identifiers available, even if the actual installed version might otherwise
342 provide those identifiers.
343
344 \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
345 of the plugin binary, which is platform dependent; e.g. the library \c MyAppTypes would produce
346 \c libMyAppTypes.so on Linux and \c MyAppTypes.dll on Windows.
347
348 <Path> is an optional argument specifying either an absolute path to the directory containing the
349 plugin file, or a relative path from the directory containing the \c qmldir file to the directory
350 containing the plugin file. By default the engine searches for the plugin library in the directory that contains the \c qmldir
351 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.
352
353
354 \section1 Debugging
355
356 The \c QML_IMPORT_TRACE environment variable can be useful for debugging
357 when there are problems with finding and loading modules. See
358 \l{Debugging module imports} for more information.
359
360
361 */
362 /