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