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