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
directoryimports.qdoc
Go to the documentation of this file.
1
// Copyright (C) 2017 The Qt Company Ltd.
2
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GFDL-1.3-no-invariants-only
3
/*!
4
\page qtqml-syntax-directoryimports.html
5
\title Importing QML Document Directories
6
\brief Description of directory import statements in QML
7
8
A local directory of QML files can be imported without any additional setup or
9
configuration. A remote directory of QML files can also be imported, but
10
requires a directory listing \c qmldir file to exist. A local directory may
11
optionally contain a directory listing \c qmldir file in order to define the
12
type names which should be provided to clients which import the directory, and
13
to specify JavaScript resources which should be made available to importers.
14
15
16
\section1 Local Directory Imports
17
18
Any QML file on the local file system can import a local directory as using an
19
import statement that refers to the directory's absolute or relative file
20
system path, enabling the file to use the \l{qtqml-typesystem-objecttypes.html}
21
{object types} defined within that directory.
22
23
If the local directory contains a directory listing \c qmldir file, the types
24
will be made available with the type names specified in the \c qmldir file;
25
otherwise, they will be made available with type names derived from the
26
filenames of the QML documents. Only filenames beginning with an uppercase
27
letter and ending with ".qml" will be exposed as types if no \c qmldir file
28
is specified in the directory.
29
30
Directory Imports rank below any
31
\l{qtqml-modules-identifiedmodules.html}{module imports} in precedence. If the
32
same name is defined in a module and in a directory that are both imported into
33
the same namespace, only the module's type is made available.
34
35
\section2 An Example
36
37
Consider the following QML project directory structure. Under the top level directory \c myapp,
38
there are a set of common UI components in a sub-directory named \c mycomponents, and the main
39
application code in a sub-directory named \c main, like this:
40
41
\code
42
myapp
43
|- mycomponents
44
|- CheckBox.qml
45
|- DialogBox.qml
46
|- Slider.qml
47
|- main
48
|- application.qml
49
\endcode
50
51
The \c main/application.qml file can import the \c mycomponents directory using
52
the relative path to that directory, allowing it to use the QML object types
53
defined within that directory:
54
55
\qml
56
import "../mycomponents"
57
58
DialogBox {
59
CheckBox {
60
// ...
61
}
62
Slider {
63
// ...
64
}
65
}
66
\endqml
67
68
The directory may be imported into a qualified local namespace, in which case
69
uses of any types provided in the directory must be qualified:
70
71
\qml
72
import "../mycomponents" as MyComponents
73
74
MyComponents.DialogBox {
75
// ...
76
}
77
\endqml
78
79
The ability to import a local directory is convenient for cases such as
80
in-application component sets and application prototyping, although any code
81
that imports such modules must update their relevant \c import statements
82
if the module directory moves to another location. This can be avoided if
83
\l{qtqml-modules-identifiedmodules.html}{QML modules} are used instead,
84
as an installed module is imported with a unique identifier string rather than
85
a file system path.
86
87
88
\section1 The Implicit Import
89
90
The directory a QML document resides in is automatically imported. You do
91
not have to explicitly import \c{"."} or similar.
92
93
\note You should make sure that the qmldir file that specifies the module a QML
94
document belongs to resides in the same directory as the QML document itself.
95
Otherwise the implicit import is different from the module the document belongs
96
to. Then, for example, another QML document may be a singleton in the context of
97
the module, but not a singleton in the context of the implicit import. This is a
98
frequent source of mistakes.
99
100
101
\section1 Remotely Located Directories
102
103
A directory of QML files can also be imported from a remote location if the
104
directory contains a directory listing \c qmldir file.
105
106
\note This also holds for the \l{The Implicit Import}{implicit import} of the
107
directory a QML document resides in. If your QML documents are loaded from a
108
remote location, you need to add qmldir files even if they don't contain any
109
explicit directory import statements. Otherwise your QML documents won't see
110
each other.
111
112
For example, if the \c myapp directory in the previous example was hosted at
113
"http://www.my-example-server.com", and the \c mycomponents directory
114
contained a \c qmldir file defined as follows:
115
116
\code
117
CheckBox CheckBox.qml
118
DialogBox DialogBox.qml
119
Slider Slider.qml
120
\endcode
121
122
Then, the directory could be imported using the URL to the remote
123
\c mycomponents directory:
124
125
\qml
126
import "http://www.my-example-server.com/myapp/mycomponents"
127
128
DialogBox {
129
CheckBox {
130
// ...
131
}
132
Slider {
133
// ...
134
}
135
}
136
\endqml
137
138
Note that when a file imports a directory over a network, it can only access QML
139
and JavaScript files specified in the \c qmldir file located in the directory.
140
141
\warning When importing directories from a remote server, developers should
142
always be careful to only load directories from trusted sources to avoid
143
loading malicious code.
144
145
146
\section1 Directory Listing qmldir Files
147
148
A directory listing \c qmldir file is distinctly different from a
149
\l{qtqml-modules-qmldir.html}{module definition qmldir file}. A directory
150
listing \c qmldir file allows a group of QML documents to be quickly and easily
151
shared, but it does not define a type namespace into which the QML object types
152
defined by the documents are registered, nor does it support versioning of
153
those QML object types.
154
155
The syntax of a directory listing \c qmldir file is as follows:
156
\table
157
\header
158
\li Command
159
\li Syntax
160
\li Description
161
162
\row
163
\li Object Type Declaration
164
\li <TypeName> <FileName>
165
\li An object type declaration allows a QML document to be exposed with
166
the given \c <TypeName>.
167
168
Example:
169
\code
170
RoundedButton RoundedBtn.qml
171
\endcode
172
173
\row
174
\li Internal Object Type Declaration
175
\li internal <TypeName> <FileName>
176
\li An internal object type declaration allows a QML document to be
177
registered as a type which becomes available only to the other
178
QML documents contained in the directory import. The internal
179
type will not be made available to clients who import the directory.
180
181
Example:
182
\code
183
internal HighlightedButton HighlightedBtn.qml
184
\endcode
185
186
\row
187
\li JavaScript Resource Declaration
188
\li <Identifier> <FileName>
189
\li A JavaScript resource declaration allows a JavaScript file to be
190
exposed via the given identifier.
191
192
Example:
193
\code
194
MathFunctions mathfuncs.js
195
\endcode
196
\endtable
197
198
A local file system directory may optionally include a \c qmldir file. This
199
allows the engine to only expose certain QML types to clients who import the
200
directory. Additionally, JavaScript resources in the directory are not exposed
201
to clients unless they are declared in a \c qmldir file.
202
203
*/
qtdeclarative
src
qml
doc
src
qmllanguageref
syntax
directoryimports.qdoc
Generated on
for Qt by
1.14.0