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
structure.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-documents-structure.html
5
\title Structure of a QML Document
6
\brief Description of the structure of QML documents
7
8
9
A QML document is a self contained piece of QML source code that consists of three parts:
10
11
\list
12
\li An optional list of pragmas
13
\li Its \e import statements
14
\li A single root object declaration
15
\endlist
16
17
By convention, a single empty line separates the imports from the object hierarchy definition.
18
19
QML documents are always encoded in UTF-8 format.
20
21
22
23
\keyword QML.pragma
24
\section1 Pragmas
25
26
Pragmas are instructions to the QML engine itself that can be used to specify
27
certain characteristics of objects in the current file or to modify how the
28
engine interprets code. The following pragmas are exaplained in details below.
29
30
\list
31
\li \c Singleton
32
\li \c ListPropertyAssignBehavior
33
\li \c ComponentBehavior
34
\li \c FunctionSignatureBehavior
35
\li \c NativeMethodBehavior
36
\li \c ValueTypeBehavior
37
\li \c Translator
38
\endlist
39
40
\keyword QML.Singleton
41
\section2 Singleton
42
43
\c{pragma Singleton} declares the component defined in the QML document as
44
singleton. Singletons are created only once per QML engine. In order to use
45
a QML-declared singleton you also have to register it with its module. See
46
\l{qt_target_qml_sources} for how to do this with CMake.
47
48
\keyword QML.ListPropertyAssignBehavior
49
\keyword QML.Append
50
\keyword QML.ReplaceIfNotDefault
51
\section2 ListPropertyAssignBehavior
52
53
With this pragma you can define how assignments to list properties shall be
54
handled in components defined in the QML document. By default, assigning to a
55
list property appends to the list. You can explicitly request this behavior
56
using the value \c{Append}. Alternatively, you can request the contents of list
57
properties to always be replaced using \c{Replace}, or replaced if the property
58
is not the default property using \c{ReplaceIfNotDefault}. For example:
59
60
\qml
61
pragma ListPropertyAssignBehavior: ReplaceIfNotDefault
62
\endqml
63
64
\note The same declaration can also be given for C++-defined types, by adding
65
the \l{QML_LIST_PROPERTY_ASSIGN_BEHAVIOR_APPEND},
66
\l{QML_LIST_PROPERTY_ASSIGN_BEHAVIOR_REPLACE}, and
67
\l{QML_LIST_PROPERTY_ASSIGN_BEHAVIOR_REPLACE_IF_NOT_DEFAULT} macros to the
68
class declaration.
69
70
\keyword QML.ComponentBehavior
71
\keyword QML.Bound
72
\keyword QML.Unbound
73
\section2 ComponentBehavior
74
75
You may have multiple components defined in the same QML file. The root
76
scope of the QML file is a component, and you may additionally have
77
elements of type \l QQmlComponent, explicitly or implicitly created
78
as properties, or inline components. Those components are nested. Each
79
of the inner components is within one specific outer component. Most of
80
the time, IDs defined in an outer component are accessible within all
81
its nested inner components. You can, however, create elements from a
82
component in any a different context, with different IDs available.
83
Doing so breaks the assumption that outer IDs are available. Therefore,
84
the engine and the QML tooling cannot generally know in advance what
85
type, if any, such IDs will resolve to at run time.
86
87
With the ComponentBehavior pragma you can restrict all inner components
88
defined in a file to only create objects within their original context.
89
If a component is bound to its context, you can safely use IDs from
90
outer components in the same file within the component. QML tooling will
91
then assume the outer IDs with their specific types to be available.
92
93
In order to bind the components to their context specify the \c{Bound}
94
argument:
95
96
\qml
97
pragma ComponentBehavior: Bound
98
\endqml
99
100
This implies that, in case of name clashes, IDs defined outside a bound
101
component override local properties of objects created from the
102
component. Otherwise it wouldn't actually be safe to use the IDs since
103
later versions of a module might add more properties to the component.
104
If the component is not bound, local properties override IDs defined
105
outside the component, but not IDs defined inside the component.
106
107
The example below prints the \e r property of the ListView object with
108
the id \e color, not the \e r property of the rectangle's color.
109
110
\qml
111
pragma ComponentBehavior: Bound
112
import QtQuick
113
114
ListView {
115
id: color
116
property int r: 12
117
model: 1
118
119
delegate: Rectangle {
120
Component.onCompleted: console.log(color.r)
121
}
122
}
123
\endqml
124
125
The default value of \c ComponentBehavior is \c{Unbound}. You can also
126
specify it explicitly. In a future version of Qt the default will change
127
to \c{Bound}.
128
129
Delegate components bound to their context don't receive their own
130
private contexts on instantiation. This means that model data can only
131
be passed via \l{Required Properties}{required properties} in this case.
132
Passing model data via context properties will not work. This concerns
133
delegates to e.g. \l{Instantiator}, \l{Repeater}, \l{ListView},
134
\l{TableView}, \l{GridView}, \l{TreeView} and in general anything that
135
uses \l{DelegateModel} internally.
136
137
For example, the following will \e{not} work:
138
139
\qml
140
pragma ComponentBehavior: Bound
141
import QtQuick
142
143
ListView {
144
delegate: Rectangle {
145
color: model.myColor
146
}
147
}
148
\endqml
149
150
The \c{delegate} property of \l{ListView} is a component. Therefore, a
151
\l{Component} is implicitly created around the \l{Rectangle} here. That
152
component is bound to its context. It doesn't receive the context property
153
\c{model} provided by \l{ListView}. To make it work, you'd have to write
154
it this way:
155
156
\qml
157
pragma ComponentBehavior: Bound
158
import QtQuick
159
160
ListView {
161
delegate: Rectangle {
162
required property color myColor
163
color: myColor
164
}
165
}
166
\endqml
167
168
You can nest components in a QML file. The pragma holds for all components in
169
the file, no matter how deeply nested.
170
171
\keyword QML.FunctionSignatureBehavior
172
\keyword QML.Ignored
173
\keyword QML.Enforced
174
\section2 FunctionSignatureBehavior
175
176
With this pragma you can change the way type annotations on functions
177
are handled. Since Qt 6.7 type annotations are enforced when calling
178
functions. Before, only the \l{QML script compiler} enforced the type
179
annotations. The interpreter and JIT compiler ignored them. Always
180
enforcing the type annotations is a behavior change in comparison to
181
earlier versions since you could call functions with mismatched
182
arguments before.
183
184
Specifying \c{Ignored} as value makes the QML engine and the
185
\l{QML script compiler} ignore any type annotations and therefore
186
restores the pre-6.7 behavior of the interpreter and JIT. As a result
187
less code is compiled to C++ ahead of time, and more code has to be
188
interpreted or JIT-compiled.
189
190
Specifying \c{Enforced} as value explicitly states the default: Type
191
annotations are always enforced.
192
193
\sa {Type annotations and assertions}
194
195
\keyword QML.NativeMethodBehavior
196
\keyword QML.AcceptThisObject
197
\keyword QML.RejectThisObject
198
\section2 NativeMethodBehavior
199
200
Calling C++ methods with \c this objects different from the one they were
201
retrieved from is broken, due to historical reasons. The original object is
202
used as \c this object. You can allow the given \c this object to be used by
203
setting \c {pragma NativeMethodBehavior: AcceptThisObject}. Specifying
204
\c RejectThisObject keeps the historical behavior.
205
206
An example of this can be found under \l {C++ methods and the 'this' object}.
207
208
\keyword QML.ValueTypeBehavior
209
\keyword QML.Addressable
210
\keyword QML.Inaddressable
211
\keyword QML.Assertable
212
\keyword QML.Copy
213
\keyword QML.Reference
214
\section2 ValueTypeBehavior
215
216
With this pragma you can change the way value types and sequences are handled.
217
218
Usually lower case names cannot be type names in JavaScript code. This is a
219
problem because value type names are lower case. You can specify \c{Addressable}
220
as value for this pragma to change this. If \c{Addressable} is specified a
221
JavaScript value can be explicitly coerced to a specific, named, value type. This is
222
done using the \c as operator, like you would do with object types. Furthermore,
223
you can also check for value types using the \c instanceof operator:
224
225
\qml
226
pragma ValueTypeBehavior: Addressable
227
import QtQml
228
229
QtObject {
230
property var a
231
property real b: (a as rect).x
232
property bool c: a instanceof rect
233
234
property var rect // inaccessible. "rect" is a type name.
235
}
236
\endqml
237
238
Since \c rect in the above example is now a type name, it will shadow any
239
properties called \c{rect}.
240
241
Explicitly casting to the desired type helps tooling. It can allow the
242
\l{Qt Quick Compiler} generate efficient code where it otherwise would not be
243
able to. You can use \l{qmllint} to find such occurrences.
244
245
There is also a \c{Inaddressable} value you can use to explicitly specify the
246
default behavior.
247
248
Another attribute to the \c{ValueTypeBehavior} pragma is \c{Assertable},
249
introduced in Qt 6.8. Due to a mistake in Qt 6.6 and 6.7 the \c{a as rect} above
250
not only checks whether \c{a} is a \c{rect} but also constructs a \c{rect} if
251
\c{a} is of a compatible type. This is obviously not what a type assertion
252
should do. Specifying \c{Assertable} prevents this behavior and restricts type
253
assertions for value types to only check for the type. You should always specify
254
it if you are going to use value types with \c{as}. In any case, if the
255
type assertion for a value type fails, the result is \c{undefined}.
256
257
\c{instanceof} does not have this problem since it only checks for inheritance,
258
not for all possible type coercions.
259
260
\note Using \c{as} with the \c{int} and \c{double} types is not advisable since by
261
JavaScript rules, the result of any calculation is a floating point number, even
262
if it happens to hold the same value as its integer equivalent. Conversely, any
263
integer constant you declare in JavaScript is not a double by QML's type mapping
264
rules. Furthermore, \c{int} and \c{double} are reserved words. You can only
265
address these types via type namespaces.
266
267
Value types and sequences are generally treated as references. This means, if
268
you retrieve a value type instance from a property into a local value, and then
269
change the local value, the original property is also changed. Furthermore,
270
if you write the original property explicitly, the local value is also updated.
271
This behavior is rather unintuitive in many places, and you should not rely on
272
it. The \c{Copy} and \c{Reference} values for the \c{ValueTypeBehavior} pragma
273
are experimental options to change this behavior. You should not use them.
274
Specifying \c{Copy} causes all value types to be treated as actual copies.
275
Specifying \c{Reference} explicitly states the default behavior.
276
277
Rather than using \c{Copy} you should explicitly re-load references to value
278
types and sequences any time they can have been affected by side effects. Side
279
effects can happen whenever you call a function or imperatively set a property.
280
\l{qmllint} provides guidance on this. For example, in the following code
281
the variable \c f is affected by side effects after writing \c width. This is
282
because there may be a binding in a derived type or in a \c Binding element
283
that updates \c font when \c width is changed.
284
285
\qml
286
import QtQuick
287
Text {
288
function a() : real {
289
var f = font;
290
width = f.pixelSize;
291
return f.pointSize;
292
}
293
}
294
\endqml
295
296
In order to address this, you can avoid holding \c f across the write operation
297
on \c width:
298
299
\qml
300
import QtQuick
301
Text {
302
function a() : real {
303
var f = font;
304
width = f.pixelSize;
305
f = font;
306
return f.pointSize;
307
}
308
}
309
\endqml
310
311
This, in turn can be shortened to:
312
313
\qml
314
import QtQuick
315
Text {
316
function a() : real {
317
width = font.pixelSize;
318
return font.pointSize;
319
}
320
}
321
\endqml
322
323
You might assume that re-retrieving the \c font property is costly, but actually
324
the QML engine automatically refreshes value type references each time you read
325
from them. So this is not more expensive than the first version, but a clearer
326
way to express the same operations.
327
328
\sa {Type annotations and assertions}
329
330
\keyword QML.Translator
331
\section2 Translator
332
333
With this pragma you can set the context for the translations in the file.
334
335
\qml
336
pragma Translator: myTranslationContext
337
\endqml
338
339
\qml
340
pragma Translator: "myTranslationContext"
341
\endqml
342
343
For more information on internationalization with QML, see \l{QML: Use qsTr()}{Use qsTr}.
344
345
\section1 Imports
346
347
A document must import the necessary modules or type namespaces to enable the
348
engine to load the QML object types referenced within the document. By default,
349
a document can access any QML object types that have been defined through
350
\c .qml files in the same directory; if a document needs to refer to any other
351
object types, it must import the type namespace into which those types have
352
been registered.
353
354
QML does \e not have a preprocessor that modifies the document prior to
355
presentation to the \l{QQmlEngine}{QML engine}, unlike C or C++.
356
The \c import statements do not copy and prepend the code in the document, but
357
instead instruct the QML engine on how to resolve type references found
358
in the document. Any type reference present in a QML document - such as \c
359
Rectangle and \c ListView - including those made within a \l {JavaScript
360
Expressions in QML Documents}{JavaScript block} or \l {Property Binding}{property
361
bindings}, are \e resolved based exclusively on the import statements. At least
362
one \c import statement must be present such as \c{import QtQuick 2.0}.
363
364
Please see the \l{qtqml-syntax-imports.html}{QML Syntax - Import Statements}
365
documentation for in-depth information about QML imports.
366
367
368
\section1 The Root Object Declaration
369
370
A QML document describes a hierarchy of objects which can be instantiated.
371
Each object definition has a certain structure; it has a type, it can have an
372
id and an object name, it can have properties, it can have methods, it can have
373
signals and it can have signal handlers.
374
375
A QML file must only contain \b {a single root object definition}. The following is invalid and will generate an error:
376
377
\code
378
// MyQmlFile.qml
379
import QtQuick 2.0
380
381
Rectangle { width: 200; height: 200; color: "red" }
382
Rectangle { width: 200; height: 200; color: "blue" } // invalid!
383
\endcode
384
385
This is because a .qml file automatically defines a QML type, which encapsulates a \e single QML object definition. This is discussed further in \l{qtqml-documents-definetypes.html}{Documents as QML object type definitions}.
386
387
*/
qtdeclarative
src
qml
doc
src
qmllanguageref
documents
structure.qdoc
Generated on
for Qt by
1.14.0