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
references.qdoc
Go to the documentation of this file.
1
// Copyright (C) 2023 The Qt Company Ltd.
2
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GFDL-1.3-no-invariants-only
3
/*!
4
\page qtqml-typesystem-references.html
5
\title QML Value Type and Sequence References
6
\brief Description of QML value type and sequence references
7
8
\l{QML Value Types} and \l{QML Sequence Types} are necessarily passed by value.
9
In contrast to \l{QML Object Types} they have no identity of themselves, but can
10
only be accessed as properties of other objects or values, or as values returned
11
from methods. Each such access implicitly creates a copy. Yet, in JavaScript
12
everything is an object. There is no such concept as a value type in JavaScript.
13
For example, if you execute \c{font.bold = true} in JavaScript, we expect the \c bold
14
property of \c font to be set, no matter what \c font is. But consider the following
15
code snippet:
16
17
\qml
18
import QtQuick
19
Text {
20
onSomethingHappened: font.bold = true
21
}
22
\endqml
23
24
In this case we know that \c font is a value type. Accessing it creates a local copy
25
by calling the getter of a \l{Q_PROPERTY}. We can then set the \c bold property on it,
26
but that would usually only affect the copy, not the original \l{Q_PROPERTY}.
27
28
To overcome this problem, QML offers the concept of references. When you retrieve
29
an instance of a value or sequence type from a property, the QML engine remembers
30
the property along with the value itself. If the value is modified, it is written
31
back to the property. This produces the illusion of an object with separate identity
32
and makes the above case, along with many others, just work.
33
34
This can be rather expensive, though. If a sequence is exposed as a Q_PROPERTY,
35
accessing any value in the sequence by index will cause the whole sequence data
36
to be read from the property. From this sequence data, a single element is then
37
retrieved. Similarly, modifying any value in the sequence causes the
38
sequence data to be read. Then the modification is performed and the modified
39
sequence is be written back to the property. A read operation can be relatively
40
cheap if the type in question is implicitly shared. A modification always incurs
41
at least one deep copy.
42
43
If you return an instance of a sequence or value type from a \l Q_INVOKABLE function
44
you avoid such overhead. Return values are not attached to any property and won't be
45
written back.
46
47
Sequences of object types are passed as \l{QQmlListProperty} by default.
48
\l{QQmlListProperty} is not an actual container, but only a view, or reference, to
49
some sequential storage. Therefore, \l{QQmlListProperty} is not affected by this
50
effect. You can, however, register other sequence types for objects using
51
\l{QML_SEQUENTIAL_CONTAINER}. Those will be affected.
52
53
*/
qtdeclarative
src
qml
doc
src
qmllanguageref
typesystem
references.qdoc
Generated on
for Qt by
1.14.0