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