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
9For every \l{qtqml-typesystem-objecttypes.html}{object type} and
10\l{qtqml-typesystem-valuetypes.html}{value type} a sequence type for storing
11multiple instances of the type is automatically made available. You can use
12the \c list keyword to create properties of sequence types:
13
14\qml
15import QtQml
16
17QtObject {
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
30Sequences of value types are implemented as \l{QList} and sequences of object
31types are implemented as \l{QQmlListProperty}.
32
33Sequences in QML generally behave like the JavaScript \c Array type, with some
34important differences which result from the use of a C++ storage type in the
35implementation:
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
49If you wish to remove elements from a sequence rather than simply replace
50them 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
54In general any container recognizable by \l QMetaSequence can be passed from
55C++ to QML via \l Q_PROPERTY or \l Q_INVOKABLE methods. This includes, but is
56not limited to, all registered QList, QQueue, QStack, QSet, std::list,
57std::vector that contain a type marked with \l Q_DECLARE_METATYPE.
58
59Using a sequence via \l QMetaSequence results in expensive data conversions.
60To avoid the conversions you can register your own anonymous sequence types
61using \l{QML_SEQUENTIAL_CONTAINER} from C++. Types registered this way behave
62like the pre-defined sequence types and are stored as-is. However, they have
63no QML names.
64
65\warning Sequences stored as a C++ container like \l QList or \c std::vector are
66subject to the effects caused by \l{QML Value Type and Sequence References} and
67should thus be handled with care. \l QQmlListProperty is not affected since
68it 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
70produces a deep copy. Since a sequence read from a property always has to be
71copied at least once, using such containers as QML sequences is rather
72expensive, even if you don't modify them from QML.
73
74The QtQml module contains a few \l [QML] {QtQml#Sequence Types}{sequence types}
75you may want to use.
76
77*/