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
qqmllist.h
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3// Qt-Security score:significant
4
5#ifndef QQMLLIST_H
6#define QQMLLIST_H
7
8#include <QtQml/qtqmlglobal.h>
9
10#include <QtCore/qcontainerinfo.h>
11#include <QtCore/qlist.h>
12#include <QtCore/qmetatype.h>
13#include <QtCore/qvariant.h>
14
15QT_BEGIN_NAMESPACE
16
17class QObject;
18struct QMetaObject;
19
20#define QML_LIST_PROPERTY_ASSIGN_BEHAVIOR_APPEND Q_CLASSINFO("QML.ListPropertyAssignBehavior", "Append")
21#define QML_LIST_PROPERTY_ASSIGN_BEHAVIOR_REPLACE_IF_NOT_DEFAULT Q_CLASSINFO("QML.ListPropertyAssignBehavior", "ReplaceIfNotDefault")
22#define QML_LIST_PROPERTY_ASSIGN_BEHAVIOR_REPLACE Q_CLASSINFO("QML.ListPropertyAssignBehavior", "Replace")
23
24template<typename T>
26public:
27 using value_type = T*;
28
29 using AppendFunction = void (*)(QQmlListProperty<T> *, T *);
31 using AtFunction = T *(*)(QQmlListProperty<T> *, qsizetype);
32 using ClearFunction = void (*)(QQmlListProperty<T> *);
33 using ReplaceFunction = void (*)(QQmlListProperty<T> *, qsizetype, T *);
34 using RemoveLastFunction = void (*)(QQmlListProperty<T> *);
35
36 QQmlListProperty() = default;
37
38 QQmlListProperty(QObject *o, QList<T *> *list)
39 : object(o), data(list), append(qlist_append), count(qlist_count), at(qlist_at),
40 clear(qlist_clear), replace(qlist_replace), removeLast(qlist_removeLast)
41 {}
42
43 QQmlListProperty(QObject *o, void *d, AppendFunction a, CountFunction c, AtFunction t,
45 : object(o),
46 data(d),
47 append(a),
48 count(c),
49 at(t),
50 clear(r),
51 replace((a && c && t && r) ? qslow_replace : nullptr),
52 removeLast((a && c && t && r) ? qslow_removeLast : nullptr)
53 {}
54
55 QQmlListProperty(QObject *o, void *d, AppendFunction a, CountFunction c, AtFunction t,
57 : object(o),
58 data(d),
59 append(a),
60 count(c),
61 at(t),
62 clear((!r && p && c) ? qslow_clear : r),
63 replace((!s && a && c && t && (r || p)) ? qslow_replace : s),
64 removeLast((!p && a && c && t && r) ? qslow_removeLast : p)
65 {}
66
67 QQmlListProperty(QObject *o, void *d, CountFunction c, AtFunction a)
68 : object(o), data(d), count(c), at(a)
69 {}
70
71 bool operator==(const QQmlListProperty &o) const {
72 return object == o.object &&
73 data == o.data &&
74 append == o.append &&
75 count == o.count &&
76 at == o.at &&
77 clear == o.clear &&
78 replace == o.replace &&
79 removeLast == o.removeLast;
80 }
81
82 QObject *object = nullptr;
83 void *data = nullptr;
84
87 AtFunction at = nullptr;
91
92 template<typename List>
93 List toList()
94 {
95 if constexpr (std::is_same_v<List, QList<T *>>) {
96 if (append == qlist_append)
97 return *static_cast<QList<T *> *>(data);
98 }
99 return toList_impl<List>();
100 }
101
102private:
103 template<typename List>
104 List toList_impl()
105 {
106 const qsizetype size = count(this);
107
108 List result;
109 if constexpr (QContainerInfo::has_reserve_v<List>)
110 result.reserve(size);
111
112 static_assert(QContainerInfo::has_push_back_v<List>);
113 for (qsizetype i = 0; i < size; ++i)
114 result.push_back(at(this, i));
115
116 return result;
117 }
118
119private:
120 static void qlist_append(QQmlListProperty *p, T *v) {
121 static_cast<QList<T *> *>(p->data)->append(v);
122 }
123 static qsizetype qlist_count(QQmlListProperty *p) {
124 return static_cast<QList<T *> *>(p->data)->size();
125 }
126 static T *qlist_at(QQmlListProperty *p, qsizetype idx) {
127 return static_cast<QList<T *> *>(p->data)->at(idx);
128 }
129 static void qlist_clear(QQmlListProperty *p) {
130 return static_cast<QList<T *> *>(p->data)->clear();
131 }
132 static void qlist_replace(QQmlListProperty *p, qsizetype idx, T *v) {
133 return static_cast<QList<T *> *>(p->data)->replace(idx, v);
134 }
135 static void qlist_removeLast(QQmlListProperty *p) {
136 return static_cast<QList<T *> *>(p->data)->removeLast();
137 }
138
139 static void qslow_replace(QQmlListProperty<T> *list, qsizetype idx, T *v)
140 {
141 const qsizetype length = list->count(list);
142 if (idx < 0 || idx >= length)
143 return;
144
145 QList<T *> stash;
146 if (list->clear != qslow_clear) {
147 stash.reserve(length);
148 for (qsizetype i = 0; i < length; ++i)
149 stash.append(i == idx ? v : list->at(list, i));
150 list->clear(list);
151 for (T *item : std::as_const(stash))
152 list->append(list, item);
153 } else {
154 stash.reserve(length - idx - 1);
155 for (qsizetype i = length - 1; i > idx; --i) {
156 stash.append(list->at(list, i));
157 list->removeLast(list);
158 }
159 list->removeLast(list);
160 list->append(list, v);
161 while (!stash.isEmpty())
162 list->append(list, stash.takeLast());
163 }
164 }
165
166 static void qslow_clear(QQmlListProperty<T> *list)
167 {
168 for (qsizetype i = 0, end = list->count(list); i < end; ++i)
169 list->removeLast(list);
170 }
171
172 static void qslow_removeLast(QQmlListProperty<T> *list)
173 {
174 const qsizetype length = list->count(list) - 1;
175 if (length < 0)
176 return;
177 QList<T *> stash;
178 stash.reserve(length);
179 for (qsizetype i = 0; i < length; ++i)
180 stash.append(list->at(list, i));
181 list->clear(list);
182 for (T *item : std::as_const(stash))
183 list->append(list, item);
184 }
185};
186
187class QQmlEngine;
189class Q_QML_EXPORT QQmlListReference
190{
191public:
192 QQmlListReference();
193
194#if QT_DEPRECATED_SINCE(6, 4)
195 QT_DEPRECATED_X("Drop the QQmlEngine* argument")
196 QQmlListReference(const QVariant &variant, [[maybe_unused]] QQmlEngine *engine);
197
198 QT_DEPRECATED_X("Drop the QQmlEngine* argument")
199 QQmlListReference(QObject *o, const char *property, [[maybe_unused]] QQmlEngine *engine);
200#endif
201
202 explicit QQmlListReference(const QVariant &variant);
203 QQmlListReference(QObject *o, const char *property);
204 QQmlListReference(const QQmlListReference &);
205 QQmlListReference &operator=(const QQmlListReference &);
206 ~QQmlListReference();
207
208 bool isValid() const;
209
210 QObject *object() const;
211 const QMetaObject *listElementType() const;
212
213 bool canAppend() const;
214 bool canAt() const;
215 bool canClear() const;
216 bool canCount() const;
217 bool canReplace() const;
218 bool canRemoveLast() const;
219
220 bool isManipulable() const;
221 bool isReadable() const;
222
223 bool append(QObject *) const;
224 QObject *at(qsizetype) const;
225 bool clear() const;
226 qsizetype count() const;
227 qsizetype size() const { return count(); }
228 bool replace(qsizetype, QObject *) const;
229 bool removeLast() const;
230 bool operator==(const QQmlListReference &other) const {return d == other.d;}
231
232private:
233 friend class QQmlListReferencePrivate;
234 QQmlListReferencePrivate* d;
235};
236
237namespace QtPrivate {
238template<typename T>
239inline constexpr bool IsQmlListType<QQmlListProperty<T>> = true;
240}
241
242QT_END_NAMESPACE
243
244Q_DECLARE_METATYPE(QQmlListReference)
245
246#endif // QQMLLIST_H
The QQmlListProperty class allows applications to expose list-like properties of QObject-derived clas...
Definition qqmllist.h:25
T *(*)(QQmlListProperty< T > *, qsizetype) AtFunction
Synonym for {T *(*)(QQmlListProperty<T> *property, qsizetype index)}.
Definition qqmllist.h:31
QQmlListProperty(QObject *o, QList< T * > *list)
Definition qqmllist.h:38
ClearFunction clear
Definition qqmllist.h:88
bool operator==(const QQmlListProperty &o) const
Returns true if this QQmlListProperty is equal to other, otherwise false.
Definition qqmllist.h:71
QQmlListProperty(QObject *o, void *d, AppendFunction a, CountFunction c, AtFunction t, ClearFunction r)
Construct a QQmlListProperty from a set of operation functions append, count, at, and clear.
Definition qqmllist.h:43
QQmlListProperty(QObject *o, void *d, CountFunction c, AtFunction a)
Construct a readonly QQmlListProperty from a set of operation functions count and at.
Definition qqmllist.h:67
void(*)(QQmlListProperty< T > *) RemoveLastFunction
Synonym for {void (*)(QQmlListProperty<T> *property)}.
Definition qqmllist.h:34
AtFunction at
Definition qqmllist.h:87
QQmlListProperty(QObject *o, void *d, AppendFunction a, CountFunction c, AtFunction t, ClearFunction r, ReplaceFunction s, RemoveLastFunction p)
Construct a QQmlListProperty from a set of operation functions append, count, at, clear,...
Definition qqmllist.h:55
void(*)(QQmlListProperty< T > *, qsizetype, T *) ReplaceFunction
Synonym for {void (*)(QQmlListProperty<T> *property, qsizetype index, T *value)}.
Definition qqmllist.h:33
QObject * object
Definition qqmllist.h:82
RemoveLastFunction removeLast
Definition qqmllist.h:90
void(*)(QQmlListProperty< T > *, T *) AppendFunction
Synonym for {void (*)(QQmlListProperty<T> *property, T *value)}.
Definition qqmllist.h:29
CountFunction count
Definition qqmllist.h:86
AppendFunction append
Definition qqmllist.h:85
QQmlListProperty()=default
\macro QML_LIST_PROPERTY_ASSIGN_BEHAVIOR_APPEND
ReplaceFunction replace
Definition qqmllist.h:89
void(*)(QQmlListProperty< T > *) ClearFunction
Synonym for {void (*)(QQmlListProperty<T> *property)}.
Definition qqmllist.h:32
const QMetaObject * elementType()
Definition qqmllist_p.h:47
The QQmlListReference class allows the manipulation of \l QQmlListProperty properties.
Definition qqmllist.h:190
Combined button and popup list for selecting options.
static QT_BEGIN_NAMESPACE bool isObjectCompatible(QObject *object, QQmlListReferencePrivate *d)
Definition qqmllist.cpp:11