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