source: trunk/doc/src/declarative/modules.qdoc@ 1036

Last change on this file since 1036 was 846, checked in by Dmitry A. Kuminov, 14 years ago

trunk: Merged in qt 4.7.2 sources from branches/vendor/nokia/qt.

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