![]() |
Home · All Classes · All Functions · Overviews |
A module is a collection of QML types.
To use types from a module it must be imported using the import statement. Successive import statements override earlier import statements, however, since imports have version qualifiers, changes in modules do not alter the semantics of imports.
Types defined in C++ can be from types your application defines, standard QML types, or types defined in plugins. To use any such types, you must import the module defining them. For example, to use types from Qt, import it:
import Qt 4.6
This makes available all types in Qt that were available in Qt 4.6, regardless of the actual version of Qt executing the QML. So even if Qt 4.7 adds a type that would conflict with a type you defined while using 4.6, that type is not imported, so there is no conflict.
Types defined by plugins are made using QDeclarativeExtensionPlugin. Installed plugins and QML files can both contribute types to the same module.
When importing types defined using QML, the syntax depends on whether or not the types are installed on the system.
To import types defined in QML files that are installed on the system running the QML, a URI import is used:
import com.nokia.Example 1.0
Files imported in this way are found on the paths added by QDeclarativeEngine::addImportPath(), which by default only inludes $QTDIR/qml, so the above would make available those types defined in $QTDIR/qml/com/nokia/Example which are specified as being in version 1.0. Installed plugins and QML files can both contribute types to the same module.
The specification of types to versions is given by a special file, qmldir which must exist in the module directory. The syntax is described below.
The -L option to the viewer application also adds paths to the import path.
To import types defined in QML files in directories relative to the file importing them, a quoted import directory is used:
import "path"
This allows all components defined in the directory path to be used in the component where this statement appears.
In this case, and only this case, it is not necessary for the module directory to include a qmldir file, nor is it necessary to provide a version qualifier. The basis of this is that the files in the subdirectory are assumed to be packaged with the importer, and therefore they form a single versioned unit.
To import types defined in QML file at arbitrary network locations, a quoted absolute URL is used:
import "http://url/.../" 1.0
This works the same as for relative directory imports, except that the target location must include a qmldir file, and a version qualifier must be given.
Directories of installed files and remote content must include a file qmldir which specifies the mapping from all type names to versioned QML files. It is a list of lines of the form:
# <Comment> <TypeName> <InitialVersion> <File>
<TypeName> is the type being made available; <InitialVersion> is a version number like 4.0; <File> is the (relative) file name of the QML file defining the type.
The same type can be provided by different files in different versions, in which case later earlier versions (eg. 1.2) must precede earlier versions (eg. 1.0), since the first name-version match is used.
Installed files do not need to import the module of which they are a part, as they can refer to the other QML files in the module as relative (local) files.
Installed and remote files must be referred to by version information described above, local files may have it.
The versioning system ensures that a given QML file will work regardless of the version of installed software, since a versioned import only imports types for that version, leaving other identifiers available, even if the actual installed version might otherwise use those identifiers.
When importing content it by default imports types 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.
To import a module into a namespace:
import Qt 4.6 as TheQtLibrary
Types from Qt 4.6 may then be used, but only by qualifying them with the namespace:
TheQtLibrary.Rectangle { ... }
Multiple modules can be imported into the same namespace in the same way that multiple modules can be imported into the global namespace:
import Qt 4.6 as Nokia import Ovi 1.0 as Nokia
Copyright © 2010 Nokia Corporation and/or its subsidiary(-ies) | Trademarks | Qt 4.7.0 |