Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
imports.qdoc
Go to the documentation of this file.
1
// Copyright (C) 2021 The Qt Company Ltd.
2
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GFDL-1.3-no-invariants-only
3
/*!
4
\page qtqml-syntax-imports.html
5
\title Import Statements
6
\brief Description of import statements in QML
7
\keyword QML.import
8
9
\section1 Syntax of an Import Statement
10
11
An import statement allows clients to tell the engine which modules, JavaScript
12
resources and component directories are used within a QML document. The types
13
which may be used within a document depends on which modules, resources and
14
directories are imported by the document.
15
16
There are three different types of imports. Each import type has a slightly
17
different syntax, and different semantics apply to different import types.
18
19
\section2 Module (Namespace) Imports
20
21
The most common type of import is a module import. Clients can import
22
\l{qtqml-modules-identifiedmodules.html}{QML modules} which register QML object
23
types and JavaScript resources into a given namespace.
24
25
The generic form of a module import is as follows:
26
\code
27
import <ModuleIdentifier> [<Version.Number>] [as <Qualifier>]
28
\endcode
29
30
\list
31
\li The \c <ModuleIdentifier> is an identifier specified in dotted URI
32
notation, which uniquely identifies the type namespace provided by the
33
module.
34
\li The \c <Version.Number> is a version of the form
35
\c {MajorVersion.MinorVersion} which specifies which definitions of
36
various object types and JavaScript resources will be made available due
37
to the import. It can be omitted, in which case the latest version of the
38
module is imported. It is also possible to only omit the minor version.
39
Then the latest minor version of the given major version is imported.
40
\li The \c <Qualifier> is an optional local namespace identifier into which
41
the object types and JavaScript resources provided by the module will be
42
installed, if given. If omitted, the object types and JavaScript
43
resources provided by the module will be installed into the global
44
namespace.
45
\endlist
46
47
An example of an unqualified module import is as follows:
48
\code
49
import QtQuick
50
\endcode
51
52
This import allows the use of all of the types provided by the \c QtQuick
53
module without needing to specify a qualifier. For example, the client code to
54
create a rectangle is as follows:
55
56
\qml
57
import QtQuick
58
59
Rectangle {
60
width: 200
61
height: 100
62
color: "red"
63
}
64
\endqml
65
66
An example of an unqualified import with version would be
67
\code
68
import QtQuick 2.10
69
\endcode
70
In that case, any types defined in QtQuick 2.11 and higher or in any higher major
71
version, like 6.0, would not be available to the file.
72
73
An example of a qualified module import is as follows:
74
\code
75
import QtQuick as Quick
76
\endcode
77
78
This import allows multiple modules which provide conflicting type names to be
79
imported at the same time, however since each usage of a type provided by a
80
module which was imported into a qualified namespace must be preceded by the
81
qualifier, the conflict is able to be resolved unambiguously by the QML engine.
82
83
An example of client code which creates a rectangle after using a qualified
84
module import is as follows:
85
86
\qml
87
import QtQuick as Quick
88
89
Quick.Rectangle {
90
width: 200
91
height: 100
92
color: "red"
93
}
94
\endqml
95
96
For more information about qualified imports, see the upcoming section on
97
\l{Importing Into A Qualified Local Namespace}.
98
99
Note that if a QML document does not import a module which provides a
100
particular QML object type, but attempts to use that object type anyway,
101
an error will occur. For example, the following QML document does not
102
import \c QtQuick and thus attempting to use the \c Rectangle type will fail:
103
104
\qml
105
Rectangle {
106
width: 200
107
height: 100
108
color: "red"
109
}
110
\endqml
111
112
In this case, the engine will emit an error and refuse to load the file.
113
114
\section3 C++ Module Imports
115
116
Usually, C++ types are declared using the QML_ELEMENT and QML_NAMED_ELEMENT()
117
macros and registered via the build system using QML_IMPORT_NAME and
118
QML_IMPORT_MAJOR_VERSION. The import name and version given this way form a
119
module that can be imported to access the types.
120
121
This is most common in client applications which define their own QML object
122
types in C++.
123
124
\section3 Importing into a Qualified Local Namespace
125
126
The \c import statement may optionally use the \c as keyword to specify that
127
the types should be imported into a particular document-local namespace. If a
128
namespace is specified, then any references to the types made available by the
129
import must be prefixed by the local namespace qualifier.
130
131
Below, the \c QtQuick module is imported into the namespace "CoreItems". Now, any
132
references to types from the \c QtQuick module must be prefixed with the
133
\c CoreItems name:
134
135
\qml
136
import QtQuick as CoreItems
137
138
CoreItems.Rectangle {
139
width: 100; height: 100
140
141
CoreItems.Text { text: "Hello, world!" }
142
143
// WRONG! No namespace prefix - the Text type won't be found
144
Text { text: "Hello, world!" }
145
}
146
\endqml
147
148
A namespace acts as an identifier for a module within the scope of the file.
149
The namespace does not become an attribute of the root object that can be
150
referred to externally as can be done with properties, signals and methods.
151
152
The namespaced import is useful if there is a requirement to use two QML types
153
that have the same name but are located in different modules. In this case the
154
two modules can be imported into different namespaces to ensure the code is
155
referring to the correct type:
156
157
\qml
158
import QtQuick as CoreItems
159
import "../textwidgets" as MyModule
160
161
CoreItems.Rectangle {
162
width: 100; height: 100
163
164
MyModule.Text { text: "Hello from my custom text item!" }
165
CoreItems.Text { text: "Hello from Qt Quick!" }
166
}
167
\endqml
168
169
Note that multiple modules can be imported into the same namespace in the same
170
way that multiple modules can be imported into the global namespace. For example:
171
172
\snippet qml/imports/merged-named-imports.qml imports
173
174
\section2 Directory Imports
175
176
A directory which contains QML documents may also be imported directly in a
177
QML document. This provides a simple way for QML types to be segmented into
178
reusable groupings: directories on the filesystem.
179
180
The generic form of a directory import is as follows:
181
\qml
182
import "<DirectoryPath>" [as <Qualifier>]
183
\endqml
184
185
\note Import paths are network transparent: applications can import documents
186
from remote paths just as simply as documents from local paths. See the general
187
URL resolution rules for \l{qtqml-documents-networktransparency.html}
188
{Network Transparency} in QML documents. If the directory is remote, it must
189
contain a \l{qtqml-syntax-directoryimports.html#directory-listing-qmldir-files}
190
{directory import listing qmldir file} as the QML engine cannot determine
191
the contents of a remote directory if that \c qmldir file does not exist.
192
193
Similar semantics for the \c <Qualifier> apply to directory imports as for
194
module imports; for more information on the topic, please see the previous
195
section about \l{Importing into a Qualified Local Namespace}.
196
197
For more information about directory imports, please see the in-depth
198
documentation about \l{qtqml-syntax-directoryimports.html}{directory imports}.
199
200
\section2 JavaScript Resource Imports
201
202
JavaScript resources may be imported directly in a QML document. Every
203
JavaScript resource must have an identifier by which it is accessed.
204
205
The generic form of a JavaScript resource import is as follows:
206
\code
207
import "<JavaScriptFile>" as <Identifier>
208
\endcode
209
210
Note that the \c <Identifier> must be unique within a QML document, unlike the
211
local namespace qualifier which can be applied to module imports.
212
213
\section3 JavaScript Resources from Modules
214
215
Javascript files can be provided by modules, by adding identifier
216
definitions to the \c qmldir file which specifies the module.
217
218
For example, if the \c projects.MyQMLProject.MyFunctions module is specified
219
with the following \c qmldir file, and installed into the QML import path:
220
\code
221
module projects.MyQMLProject.MyFunctions
222
SystemFunctions 1.0 SystemFunctions.js
223
UserFunctions 1.0 UserFunctions.js
224
\endcode
225
226
a client application is able to import the JavaScript resources declared in the
227
module by importing the module and using the identifier associated with a
228
declared resource:
229
230
\qml
231
import QtQuick
232
import projects.MyQMLProject.MyFunctions
233
234
Item {
235
Component.onCompleted: { SystemFunctions.cleanUp(); }
236
}
237
\endqml
238
239
If the module was imported into a document-local namespace, the JavaScript
240
resource identifiers must be prefixed with the namespace qualifier in order
241
to be used:
242
243
\qml
244
import QtQuick
245
import projects.MyQMLProject.MyFunctions as MyFuncs
246
import org.example.Functions as TheirFuncs
247
248
Item {
249
Component.onCompleted: {
250
MyFuncs.SystemFunctions.cleanUp();
251
TheirFuncs.SystemFunctions.shutdown();
252
}
253
}
254
\endqml
255
256
\section3 Further Information
257
258
For more information about JavaScript resources, please see the documentation
259
about \l{qtqml-javascript-resources.html}
260
{defining JavaScript resources in QML}, and for more information about how
261
to import JavaScript resources, and how imports can be used from within
262
JavaScript resources, please see the in-depth documentation about
263
\l{qtqml-javascript-imports.html}{importing JavaScript resources in QML}.
264
265
266
\section1 QML Import Path
267
268
When an \l{Identified Modules}{identified module} is imported,
269
the QML engine searches the \e{import path} for a matching module.
270
271
This import path, as returned by QQmlEngine::importPathList(), defines the
272
default locations to be searched by the engine. By default, this list contains,
273
in this order of precedence:
274
275
\list
276
\li Platform-specific bundle paths if applicable (for example on macOS or Android)
277
\li The directory of the application binary
278
\li The qrc:/qt-project.org/imports path inside the resources.
279
\li The qrc:/qt/qml path inside the resources (since Qt 6.5).
280
\li Paths specified by the \c QML2_IMPORT_PATH environment variable (deprecated)
281
\li Paths specified by the \c QML_IMPORT_PATH environment variable
282
\li The location specified by QLibraryInfo::QmlImportsPath
283
\endlist
284
285
If the \c{Qt::AA_PluginApplication} attribute is set on \l{QCoreApplication},
286
then the application directory, any paths specified by environment variables,
287
and any platform specific bundle paths outside the resource file system are
288
omitted by default.
289
290
Additional import paths can be added through QQmlEngine::addImportPath() or the
291
\c QML_IMPORT_PATH environment variable. When running the
292
\l {Prototyping with the QML Runtime Tool}{qml tool}, you can also use the
293
\c -I option to add an import path.
294
295
You can specify multiple import paths in the \c QML_IMPORT_PATH environment
296
variable by joining them using the path separator. On Windows the path separator
297
is a semicolon (;), on other platforms it is a colon (:). This means that you
298
cannot specify resource paths or URLs in QML_IMPORT_PATH, as they contain
299
colons themselves. However, you can add resource paths and URLs by calling
300
QQmlEngine::addImportPath() programatically.
301
302
\note It is recommended that applications and libraries put their modules
303
under "qrc:/qt/qml". This happens by default when the module is created
304
with \l{qt_add_qml_module}{qt_add_qml_module()} and \l{QTP0001} is
305
enabled.
306
307
308
\section1 Debugging
309
310
The \c QML_IMPORT_TRACE environment variable can be useful for debugging
311
when there are problems with finding and loading modules. See
312
\l{Debugging module imports} for more information.
313
314
*/
qtdeclarative
src
qml
doc
src
qmllanguageref
syntax
imports.qdoc
Generated on
for Qt by
1.14.0