1
/****************************************************************************
3
** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
4
** Contact: http://www.qt-project.org/legal
6
** This file is part of the documentation of the Qt Toolkit.
8
** $QT_BEGIN_LICENSE:FDL$
9
** Commercial License Usage
10
** Licensees holding valid commercial Qt licenses may use this file in
11
** accordance with the commercial license agreement provided with the
12
** Software or, alternatively, in accordance with the terms contained in
13
** a written agreement between you and Digia. For licensing terms and
14
** conditions see http://qt.digia.com/licensing. For further information
15
** use the contact form at http://qt.digia.com/contact-us.
17
** GNU Free Documentation License Usage
18
** Alternatively, this file may be used under the terms of the GNU Free
19
** Documentation License version 1.3 as published by the Free Software
20
** Foundation and appearing in the file included in the packaging of
21
** this file. Please review the following information to ensure
22
** the GNU Free Documentation License version 1.3 requirements
23
** will be met: http://www.gnu.org/copyleft/fdl.html.
26
****************************************************************************/
28
\page qtqml-syntax-imports.html
29
\title Import Statements
30
\brief Description of import statements in QML
32
\section1 Syntax of an Import Statement
34
An import statement allows clients to tell the engine which modules, JavaScript
35
resources and component directories are used within a QML document. The types
36
which may be used within a document depends on which modules, resources and
37
directories are imported by the document.
39
\section2 Import Types
41
There are three different types of imports. Each import type has a slightly
42
different syntax, and different semantics apply to different import types.
44
\section3 Module (Namespace) Imports
46
The most common type of import is a module import. Clients can import
47
\l{qtqml-modules-identifiedmodules.html}{QML modules} which register QML object
48
types and JavaScript resources into a given namespace.
50
The generic form of a module import is as follows:
52
import <ModuleIdentifier> <Version.Number> [as <Qualifier>]
56
\li The \c <ModuleIdentifier> is an identifier specified in dotted URI
57
notation, which uniquely identifies the type namespace provided by the
59
\li The \c <Version.Number> is a version of the form
60
\c {MajorVersion.MinorVersion} which specifies which definitions of
61
various object types and JavaScript resources will be made available due
63
\li The \c <Qualifier> is an optional local namespace identifier into which
64
the object types and JavaScript resources provided by the module will be
65
installed, if given. If omitted, the object types and JavaScript
66
resources provided by the module will be installed into the global
70
An example of an unqualified module import is as follows:
75
This import allows the use of all of the types provided by the \c QtQuick
76
module without needing to specify a qualifier. For example, the client code to
77
create a rectangle is as follows:
89
An example of a qualified module import is as follows:
91
import QtQuick 2.0 as Quick
94
This import allows multiple modules which provide conflicting type names to be
95
imported at the same time, however since each usage of a type provided by a
96
module which was imported into a qualified namespace must be preceded by the
97
qualifier, the conflict is able to be resolved unambiguously by the QML engine.
99
An example of client code which creates a rectangle after using a qualified
100
module import is as follows:
103
import QtQuick 2.0 as Quick
112
For more information about qualified imports, see the upcoming section on
113
\l{Importing Into A Qualified Local Namespace}.
115
Note that if a QML document does not import a module which provides a
116
particular QML object type, but attempts to use that object type anyway,
117
an error will occur. For example, the following QML document does not
118
import \c QtQuick and thus attempting to use the \c Rectangle type will fail:
128
In this case, the engine will emit an error and refuse to load the file.
130
\section4 Non-module Namespace Imports
132
Types can also be registered into namespaces directly via the various
133
registration functions in C++ (such as qmlRegisterType()). The types which
134
have been registered into a namespace in this way may be imported by importing
135
the namespace, as if the namespace was a module identifier.
137
This is most common in client applications which define their own QML object
138
types in C++ and register them with the QML type system manually.
140
\section4 Importing into a Qualified Local Namespace
142
The \c import statement may optionally use the \c as keyword to specify that
143
the types should be imported into a particular document-local namespace. If a
144
namespace is specified, then any references to the types made available by the
145
import must be prefixed by the local namespace qualifier.
147
Below, the QtQuick module is imported into the namespace "CoreItems". Now, any
148
references to types from the \c QtQuick module must be prefixed with the
152
import QtQuick 2.0 as CoreItems
154
CoreItems.Rectangle {
155
width: 100; height: 100
157
CoreItems.Text { text: "Hello, world!" }
159
// WRONG! No namespace prefix - the Text type won't be found
160
Text { text: "Hello, world!" }
164
A namespace acts as an identifier for a module within the scope of the file.
165
The namespace does not become an attribute of the root object that can be
166
referred to externally as can be done with properties, signals and methods.
168
The namespaced import is useful if there is a requirement to use two QML types
169
that have the same name but are located in different modules. In this case the
170
two modules can be imported into different namespaces to ensure the code is
171
referring to the correct type:
174
import QtQuick 2.0 as CoreItems
175
import "../textwidgets" as MyModule
177
CoreItems.Rectangle {
178
width: 100; height: 100
180
MyModule.Text { text: "Hello from my custom text item!" }
181
CoreItems.Text { text: "Hello from QtQuick!" }
185
Note that multiple modules can be imported into the same namespace in the same
186
way that multiple modules can be imported into the global namespace. For example:
188
\snippet qml/imports/merged-named-imports.qml imports
190
\section3 Directory Imports
192
A directory which contains QML documents may also be imported directly in a
193
QML document. This provides a simple way for QML types to be segmented into
194
reusable groupings: directories on the filesystem.
196
The generic form of a directory import is as follows:
198
import "<DirectoryPath>" [as <Qualifier>]
201
\note Import paths are network transparent: applications can import documents
202
from remote paths just as simply as documents from local paths. See the general
203
URL resolution rules for \l{qtqml-documents-networktransparency.html}
204
{Network Transparency} in QML documents. If the directory is remote, it must
205
contain a \l{qtqml-syntax-directoryimports.html#directory-listing-qmldir-files}
206
{directory import listing qmldir file} as the QML engine cannot determine
207
the contents of a remote directory if that \c qmldir file does not exist.
209
Similar semantics for the \c <Qualifier> apply to directory imports as for
210
module imports; for more information on the topic, please see the previous
211
section about \l{Importing into a Qualified Local Namespace}.
213
For more information about directory imports, please see the in-depth
214
documentation about \l{qtqml-syntax-directoryimports.html}{directory imports}.
216
\section3 JavaScript Resource Imports
218
JavaScript resources may be imported directly in a QML document. Every
219
JavaScript resource must have an identifier by which it is accessed.
221
The generic form of a JavaScript resource import is as follows:
223
import "<JavaScriptFile>" as <Identifier>
226
Note that the \c <Identifier> must be unique within a QML document, unlike the
227
local namespace qualifier which can be applied to module imports.
229
\section4 JavaScript Resources from Modules
231
Javascript files can be provided by modules, by adding identifier
232
definitions to the \c qmldir file which specifies the module.
234
For example, if the \c projects.MyQMLProject.MyFunctions module is specified
235
with the following \c qmldir file, and installed into the QML import path:
237
module projects.MyQMLProject.MyFunctions
238
SystemFunctions 1.0 SystemFunctions.js
239
UserFunctions 1.0 UserFunctions.js
242
a client application is able to import the JavaScript resources declared in the
243
module by importing the module and using the identifier associated with a
248
import projects.MyQMLProject.MyFunctions 1.0
251
Component.onCompleted: { SystemFunctions.cleanUp(); }
255
If the module was imported into a document-local namespace, the JavaScript
256
resource identifiers must be prefixed with the namespace qualifier in order
261
import projects.MyQMLProject.MyFunctions 1.0 as MyFuncs
262
import org.example.Functions 1.0 as TheirFuncs
265
Component.onCompleted: {
266
MyFuncs.SystemFunctions.cleanUp();
267
TheirFuncs.SystemFunctions.shutdown();
272
\section4 Further Information
274
For more information about JavaScript resources, please see the documentation
275
about \l{qtqml-javascript-resources.html}
276
{defining JavaScript resources in QML}, and for more information about how
277
to import JavaScript resources, and how imports can be used from within
278
JavaScript resources, please see the in-depth documentation about
279
\l{qtqml-javascript-imports.html}{importing JavaScript resources in QML}.
282
\section1 QML Import Path
284
When an \l{qtqml-modules-installedmodules.html}{installed module} is imported,
285
the QML engine searches the \e{import path} for a matching module.
287
This import path, as returned by QQmlEngine::importPathList(), defines the
288
default locations to be searched by the engine. By default, this list contains:
291
\li The directory of the current file
292
\li The location specified by QLibraryInfo::Qml2ImportsPath
293
\li Paths specified by the \c QML2_IMPORT_PATH environment variable
296
Additional import paths can be added through QQmlEngine::addImportPath() or the
297
\c QML2_IMPORT_PATH environment variable. When running the
298
\l{Prototyping with qmlscene}{qmlscene} tool, you can also use the \c -I option
299
to add an import path.
304
The \c QML_IMPORT_TRACE environment variable can be useful for debugging
305
when there are problems with finding and loading modules. See
306
\l{Debugging module imports} for more information.