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