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