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 |
|
---|
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 | \qml
|
---|
55 | import QtQuick 1.0
|
---|
56 | \endqml
|
---|
57 |
|
---|
58 | This imports version 1.0 of the "QtQuick" module into the global namespace. (The QML
|
---|
59 | library itself must be imported to use any of the \l {QML Elements}, as they
|
---|
60 | are not included in the global namespace by default.)
|
---|
61 |
|
---|
62 | The \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:
|
---|
64 | located modules (defined by a URL) and installed modules (defined by a URI).
|
---|
65 |
|
---|
66 |
|
---|
67 | \section1 Located Modules
|
---|
68 |
|
---|
69 | Located modules can reside on the local filesystem or a network resource,
|
---|
70 | and are referred to by a quoted location URL that specifies the filesystem
|
---|
71 | or network URL. They allow any directory with QML content to be imported
|
---|
72 | as a module, whether the directory is on the local filesystem or a remote
|
---|
73 | server.
|
---|
74 |
|
---|
75 | For example, a QML project may have a separate directory for a set of
|
---|
76 | custom UI components. These components can be accessed by importing the
|
---|
77 | directory 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
|
---|
87 | MyQMLProject
|
---|
88 | |- MyComponents
|
---|
89 | |- CheckBox.qml
|
---|
90 | |- Slider.qml
|
---|
91 | |- Window.qml
|
---|
92 | |- Main
|
---|
93 | |- application.qml
|
---|
94 | \endcode
|
---|
95 |
|
---|
96 | \o
|
---|
97 | \code
|
---|
98 | import "../MyComponents"
|
---|
99 |
|
---|
100 | Window {
|
---|
101 | Slider { ... }
|
---|
102 | CheckBox { ... }
|
---|
103 | }
|
---|
104 | \endcode
|
---|
105 |
|
---|
106 | \endtable
|
---|
107 |
|
---|
108 | Similarly, if the directory resided on a network source, it could
|
---|
109 | be 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 |
|
---|
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 | \code
|
---|
131 | import "http://the-server-name.com/MyQMLProject/MyComponents"
|
---|
132 |
|
---|
133 | Window {
|
---|
134 | Slider { ... }
|
---|
135 | CheckBox { ... }
|
---|
136 | }
|
---|
137 | \endcode
|
---|
138 |
|
---|
139 | with an optional "1.0" version specification. Notice the import would fail if
|
---|
140 | a later version was used, as the \c qmldir file specifies that these elements
|
---|
141 | are only available in the 1.0 version.
|
---|
142 |
|
---|
143 | Note that modules imported as a network resource allow only access to components
|
---|
144 | defined in QML files; components defined by C++ \l{QDeclarativeExtensionPlugin}{QML extension plugins}
|
---|
145 | are not available.
|
---|
146 |
|
---|
147 |
|
---|
148 | \section1 Installed modules
|
---|
149 |
|
---|
150 | Installed modules are modules that are made available through the QML import path,
|
---|
151 | as defined by QDeclarativeEngine::importPathList(), or modules defined within
|
---|
152 | C++ application code. An installed module is referred to by a URI, which allows
|
---|
153 | the module to be imported from QML code without specifying a complete filesystem
|
---|
154 | path or network resource URL.
|
---|
155 |
|
---|
156 | When importing an installed module, an un-quoted URI is
|
---|
157 | used, with a mandatory version number:
|
---|
158 |
|
---|
159 | \code
|
---|
160 | import QtQuick 1.0
|
---|
161 | import com.nokia.qml.mymodule 1.0
|
---|
162 | \endcode
|
---|
163 |
|
---|
164 | When a module is imported, the QML engine searches the QML import path for a matching
|
---|
165 | module. The root directory of the module must contain a
|
---|
166 | \l{Writing a qmldir file}{qmldir file} that defines the QML files
|
---|
167 | and/or C++ QML extension plugins that are made available to the module.
|
---|
168 |
|
---|
169 | Modules that are installed into the import path translate the URI into
|
---|
170 | directory names. For example, the qmldir file of the module \c com.nokia.qml.mymodule
|
---|
171 | must be located in the subpath \c com/nokia/qml/mymodule/qmldir somewhere in the
|
---|
172 | QML import path. In addition it is possible to store different versions of the
|
---|
173 | module in subdirectories of its own. For example, a version 2.1 of the
|
---|
174 | module 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
|
---|
176 | the module which matches best.
|
---|
177 |
|
---|
178 | The import path, as returned by QDeclarativeEngine::importPathList(), defines the default
|
---|
179 | locations to be searched by the QML engine for a matching module. By default, this list
|
---|
180 | contains:
|
---|
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 |
|
---|
188 | Additional import paths can be added through QDeclarativeEngine::addImportPath() or the
|
---|
189 | \c QML_IMPORT_PATH environment variable. When running the \l {QML Viewer}, you
|
---|
190 | can also use the \c -I option to add an import path.
|
---|
191 |
|
---|
192 |
|
---|
193 | \section2 Creating installed modules
|
---|
194 |
|
---|
195 | As an example, suppose the \c MyQMLProject directory in the \l{Located Modules}{previous example}
|
---|
196 | was located on the local filesystem at \c C:\qml\projects\MyQMLProject. The \c MyComponents
|
---|
197 | subdirectory 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
|
---|
201 | Slider 1.0 Slider.qml
|
---|
202 | CheckBox 1.0 CheckBox.qml
|
---|
203 | Window 1.0 Window.qml
|
---|
204 | \endcode
|
---|
205 |
|
---|
206 | Providing the path \c C:\qml is added to the QML import path using any of the methods listed previously,
|
---|
207 | a QML file located anywhere on the local filesystem can then import the module as shown below,
|
---|
208 | without referring to the module's absolute filesystem location:
|
---|
209 |
|
---|
210 | \qml
|
---|
211 | import projects.MyQMLProject.MyComponents 1.0
|
---|
212 |
|
---|
213 | Window {
|
---|
214 | Slider { ... }
|
---|
215 | CheckBox { ... }
|
---|
216 | }
|
---|
217 | \endqml
|
---|
218 |
|
---|
219 | Installed modules are also accessible as a network resource. If the \c C:\qml directory was hosted
|
---|
220 | as \c http://www.some-server.com/qml and this URL was added to the QML import path, the above
|
---|
221 | QML code would work just the same.
|
---|
222 |
|
---|
223 | Note that modules imported as a network resource allow only access to components
|
---|
224 | defined in QML files; components defined by C++ \l{QDeclarativeExtensionPlugin}{QML extension plugins}
|
---|
225 | are not available.
|
---|
226 |
|
---|
227 |
|
---|
228 | \section2 Creating installed modules in C++
|
---|
229 |
|
---|
230 | C++ applications can define installed modules directly within the application using qmlRegisterType().
|
---|
231 | For example, the \l {Tutorial: Writing QML extensions with C++}{Writing QML extensions with C++ tutorial}
|
---|
232 | defines a C++ class named \c PieChart and makes this type available to QML by calling qmlRegisterType():
|
---|
233 |
|
---|
234 | \qml
|
---|
235 | qmlRegisterType<PieChart>("Charts", 1, 0, "PieChart");
|
---|
236 | \endqml
|
---|
237 |
|
---|
238 | This allows the application's QML files to use the \c PieChart type by importing the declared
|
---|
239 | \c Charts module:
|
---|
240 |
|
---|
241 | \qml
|
---|
242 | import Charts 1.0
|
---|
243 | \endqml
|
---|
244 |
|
---|
245 | For \l{QDeclarativeExtensionPlugin}{QML plugins}, the
|
---|
246 | module URI is automatically passed to QDeclarativeExtensionPlugin::registerTypes(). This method
|
---|
247 | can 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}
|
---|
249 | example:
|
---|
250 |
|
---|
251 | \snippet examples/declarative/cppextensions/plugins/plugin.cpp plugin
|
---|
252 |
|
---|
253 | Once the plugin is built and installed, and includes a \l{Writing a qmldir file}{qmldir file},
|
---|
254 | the module can be imported from QML, like this:
|
---|
255 |
|
---|
256 | \code
|
---|
257 | import com.nokia.TimeExample 1.0
|
---|
258 | \endcode
|
---|
259 |
|
---|
260 | Unlike QML types defined by QML files, a QML type defined in a C++ extension plugin cannot be loaded by
|
---|
261 | a module that is imported as a network resource.
|
---|
262 |
|
---|
263 |
|
---|
264 |
|
---|
265 | \target namespaces
|
---|
266 | \section1 Namespaces: Using Named Imports
|
---|
267 |
|
---|
268 | 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.
|
---|
269 |
|
---|
270 | To 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 |
|
---|
278 | Types 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 |
|
---|
288 | Multiple 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 |
|
---|
297 | JavaScript 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 |
|
---|
308 | The qualifier ("MyScript" in the above example) must be unique within the QML document.
|
---|
309 | Unlike ordinary modules, multiple scripts cannot be imported into the same namespace.
|
---|
310 |
|
---|
311 |
|
---|
312 | \section1 Writing a qmldir file
|
---|
313 |
|
---|
314 | A \c qmldir file is a metadata file for a module that maps all type names in
|
---|
315 | the module to versioned QML files. It is required for installed modules, and
|
---|
316 | located modules that are loaded from a network source.
|
---|
317 |
|
---|
318 | It 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>
|
---|
323 | internal <TypeName> <File>
|
---|
324 | plugin <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
|
---|
331 | number, and <File> is the (relative) file name of the QML file defining the type.
|
---|
332 |
|
---|
333 | Installed files do not need to import the module of which they are a part, as they can refer
|
---|
334 | to the other QML files in the module as relative (local) files, but
|
---|
335 | if the module is imported from a remote location, those files must nevertheless be listed in
|
---|
336 | the \c qmldir file. Types which you do not wish to export to users of your module
|
---|
337 | may be marked with the \c internal keyword: \bold {internal <TypeName> <File>}.
|
---|
338 |
|
---|
339 | The same type can be provided by different files in different versions, in which
|
---|
340 | case later versions (e.g. 1.2) must precede earlier versions (e.g. 1.0),
|
---|
341 | since the \e first name-version match is used and a request for a version of a type
|
---|
342 | can be fulfilled by one defined in an earlier version of the module. If a user attempts
|
---|
343 | to import a version earlier than the earliest provided or later than the latest provided,
|
---|
344 | the import produces a runtime error, but if the user imports a version within the range of versions provided,
|
---|
345 | even if no type is specific to that version, no error will occur.
|
---|
346 |
|
---|
347 | A single module, in all versions, may only be provided in a single directory (and a single \c qmldir file).
|
---|
348 | If multiple are provided, only the first in the search path will be used (regardless of whether other versions
|
---|
349 | are provided by directories later in the search path).
|
---|
350 |
|
---|
351 | The versioning system ensures that a given QML file will work regardless of the version
|
---|
352 | of installed software, since a versioned import \e only imports types for that version,
|
---|
353 | leaving other identifiers available, even if the actual installed version might otherwise
|
---|
354 | provide 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
|
---|
357 | of 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
|
---|
361 | plugin file, or a relative path from the directory containing the \c qmldir file to the directory
|
---|
362 | containing the plugin file. By default the engine searches for the plugin library in the directory that contains the \c qmldir
|
---|
363 | 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.
|
---|
364 |
|
---|
365 |
|
---|
366 | \section1 Debugging
|
---|
367 |
|
---|
368 | The \c QML_IMPORT_TRACE environment variable can be useful for debugging
|
---|
369 | when there are problems with finding and loading modules. See
|
---|
370 | \l{Debugging module imports} for more information.
|
---|
371 |
|
---|
372 |
|
---|
373 | */
|
---|
374 | /
|
---|