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
sequencetypes.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-sequencetypes.html
5
\meta {keywords} {qmltopic}
6
\title QML Sequence Types
7
\brief Description of QML sequence types
8
9
For every \l{qtqml-typesystem-objecttypes.html}{object type} and
10
\l{qtqml-typesystem-valuetypes.html}{value type} a sequence type for storing
11
multiple instances of the type is automatically made available. You can use
12
the \c list keyword to create properties of sequence types:
13
14
\qml
15
import QtQml
16
17
QtObject {
18
property list<int> ints: [1, 2, 3, 4]
19
property list<Connection> connections: [
20
Connection {
21
// ...
22
},
23
Connection {
24
// ...
25
}
26
]
27
}
28
\endqml
29
30
Sequences of value types are implemented as \l{QList} and sequences of object
31
types are implemented as \l{QQmlListProperty}.
32
33
Sequences in QML generally behave like the JavaScript \c Array type, with some
34
important differences which result from the use of a C++ storage type in the
35
implementation:
36
37
\list 1
38
\li Deleting an element from a sequence will result in a default-constructed
39
value replacing that element, rather than an \c undefined value.
40
\li Setting the \c length property of a sequence to a value larger
41
than its current value will result in the sequence being padded out to the
42
specified length with default-constructed elements rather than \c undefined
43
elements.
44
\li The Qt container classes support signed (rather than
45
unsigned) integer indexes; thus, attempting to access any index greater
46
than the maximum number \l qsizetype can hold will fail.
47
\endlist
48
49
If you wish to remove elements from a sequence rather than simply replace
50
them with default constructed values, do not use the indexed delete operator
51
(\c{delete sequence[i]}) but instead use the \c {splice} function
52
(\c{sequence.splice(startIndex, deleteCount)}).
53
54
In general any container recognizable by \l QMetaSequence can be passed from
55
C++ to QML via \l Q_PROPERTY or \l Q_INVOKABLE methods. This includes, but is
56
not limited to, all registered QList, QQueue, QStack, QSet, std::list,
57
std::vector that contain a type marked with \l Q_DECLARE_METATYPE.
58
59
Using a sequence via \l QMetaSequence results in expensive data conversions.
60
To avoid the conversions you can register your own anonymous sequence types
61
using \l{QML_SEQUENTIAL_CONTAINER} from C++. Types registered this way behave
62
like the pre-defined sequence types and are stored as-is. However, they have
63
no QML names.
64
65
\warning Sequences stored as a C++ container like \l QList or \c std::vector are
66
subject to the effects caused by \l{QML Value Type and Sequence References} and
67
should thus be handled with care. \l QQmlListProperty is not affected since
68
it is only a view for an underlying container. C++ standard containers such as
69
\c std::vector are not implicitly shared. Therefore, copying them always
70
produces a deep copy. Since a sequence read from a property always has to be
71
copied at least once, using such containers as QML sequences is rather
72
expensive, even if you don't modify them from QML.
73
74
The QtQml module contains a few \l [QML] {QtQml#Sequence Types}{sequence types}
75
you may want to use.
76
77
*/
qtdeclarative
src
qml
doc
src
qmllanguageref
typesystem
sequencetypes.qdoc
Generated on
for Qt by
1.16.1