Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
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.
9In contrast to \l{QML Object Types} they have no identity of themselves, but can
10only be accessed as properties of other objects or values, or as values returned
11from methods. Each such access implicitly creates a copy. Yet, in JavaScript
12everything is an object. There is no such concept as a value type in JavaScript.
13For example, if you execute \c{font.bold = true} in JavaScript, we expect the \c bold
14property of \c font to be set, no matter what \c font is. But consider the following
15code snippet:
16
17\qml
18import QtQuick
19Text {
20 onSomethingHappened: font.bold = true
21}
22\endqml
23
24In this case we know that \c font is a value type. Accessing it creates a local copy
25by calling the getter of a \l{Q_PROPERTY}. We can then set the \c bold property on it,
26but that would usually only affect the copy, not the original \l{Q_PROPERTY}.
27
28To overcome this problem, QML offers the concept of references. When you retrieve
29an instance of a value or sequence type from a property, the QML engine remembers
30the property along with the value itself. If the value is modified, it is written
31back to the property. This produces the illusion of an object with separate identity
32and makes the above case, along with many others, just work.
33
34This can be rather expensive, though. If a sequence is exposed as a Q_PROPERTY,
35accessing any value in the sequence by index will cause the whole sequence data
36to be read from the property. From this sequence data, a single element is then
37retrieved. Similarly, modifying any value in the sequence causes the
38sequence data to be read. Then the modification is performed and the modified
39sequence is be written back to the property. A read operation can be relatively
40cheap if the type in question is implicitly shared. A modification always incurs
41at least one deep copy.
42
43If you return an instance of a sequence or value type from a \l Q_INVOKABLE function
44you avoid such overhead. Return values are not attached to any property and won't be
45written back.
46
47Sequences 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
49some sequential storage. Therefore, \{QQmlListProperty} is not affected by this
50effect. You can, however, register other sequence types for objects using
51\l{QML_SEQUENTIAL_CONTAINER}. Those will be affected.
52
53*/