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