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
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
4#ifndef QQMLLIST_H
5#define QQMLLIST_H
6
7#include <QtQml/qtqmlglobal.h>
8
9#include <QtCore/qcontainerinfo.h>
10#include <QtCore/qlist.h>
11#include <QtCore/qmetatype.h>
12#include <QtCore/qvariant.h>
13
15
16class QObject;
17struct QMetaObject;
18
19#define QML_LIST_PROPERTY_ASSIGN_BEHAVIOR_APPEND Q_CLASSINFO("QML.ListPropertyAssignBehavior", "Append")
20#define QML_LIST_PROPERTY_ASSIGN_BEHAVIOR_REPLACE_IF_NOT_DEFAULT Q_CLASSINFO("QML.ListPropertyAssignBehavior", "ReplaceIfNotDefault")
21#define QML_LIST_PROPERTY_ASSIGN_BEHAVIOR_REPLACE Q_CLASSINFO("QML.ListPropertyAssignBehavior", "Replace")
22
23template<typename T>
25public:
26 using value_type = T*;
27
28 using AppendFunction = void (*)(QQmlListProperty<T> *, T *);
29 using CountFunction = qsizetype (*)(QQmlListProperty<T> *);
30 using AtFunction = T *(*)(QQmlListProperty<T> *, qsizetype);
31 using ClearFunction = void (*)(QQmlListProperty<T> *);
32 using ReplaceFunction = void (*)(QQmlListProperty<T> *, qsizetype, T *);
33 using RemoveLastFunction = void (*)(QQmlListProperty<T> *);
34
35 QQmlListProperty() = default;
36
38 : object(o), data(list), append(qlist_append), count(qlist_count), at(qlist_at),
39 clear(qlist_clear), replace(qlist_replace), removeLast(qlist_removeLast)
40 {}
41
44 : object(o),
45 data(d),
46 append(a),
47 count(c),
48 at(t),
49 clear(r),
50 replace((a && c && t && r) ? qslow_replace : nullptr),
51 removeLast((a && c && t && r) ? qslow_removeLast : nullptr)
52 {}
53
56 : object(o),
57 data(d),
58 append(a),
59 count(c),
60 at(t),
61 clear((!r && p && c) ? qslow_clear : r),
62 replace((!s && a && c && t && (r || p)) ? qslow_replace : s),
63 removeLast((!p && a && c && t && r) ? qslow_removeLast : p)
64 {}
65
69
70 bool operator==(const QQmlListProperty &o) const {
71 return object == o.object &&
72 data == o.data &&
73 append == o.append &&
74 count == o.count &&
75 at == o.at &&
76 clear == o.clear &&
77 replace == o.replace &&
78 removeLast == o.removeLast;
79 }
80
81 QObject *object = nullptr;
82 void *data = nullptr;
83
86 AtFunction at = nullptr;
90
91 template<typename List>
92 List toList()
93 {
94 if constexpr (std::is_same_v<List, QList<T *>>) {
95 if (append == qlist_append)
96 return *static_cast<QList<T *> *>(data);
97 }
98
99 const qsizetype size = count(this);
100
101 List result;
102 if constexpr (QContainerInfo::has_reserve_v<List>)
103 result.reserve(size);
104
105 static_assert(QContainerInfo::has_push_back_v<List>);
106 for (qsizetype i = 0; i < size; ++i)
107 result.push_back(at(this, i));
108
109 return result;
110 }
111
112private:
113 static void qlist_append(QQmlListProperty *p, T *v) {
114 static_cast<QList<T *> *>(p->data)->append(v);
115 }
116 static qsizetype qlist_count(QQmlListProperty *p) {
117 return static_cast<QList<T *> *>(p->data)->size();
118 }
119 static T *qlist_at(QQmlListProperty *p, qsizetype idx) {
120 return static_cast<QList<T *> *>(p->data)->at(idx);
121 }
122 static void qlist_clear(QQmlListProperty *p) {
123 return static_cast<QList<T *> *>(p->data)->clear();
124 }
125 static void qlist_replace(QQmlListProperty *p, qsizetype idx, T *v) {
126 return static_cast<QList<T *> *>(p->data)->replace(idx, v);
127 }
128 static void qlist_removeLast(QQmlListProperty *p) {
129 return static_cast<QList<T *> *>(p->data)->removeLast();
130 }
131
132 static void qslow_replace(QQmlListProperty<T> *list, qsizetype idx, T *v)
133 {
134 const qsizetype length = list->count(list);
135 if (idx < 0 || idx >= length)
136 return;
137
138 QVector<T *> stash;
139 if (list->clear != qslow_clear) {
140 stash.reserve(length);
141 for (qsizetype i = 0; i < length; ++i)
142 stash.append(i == idx ? v : list->at(list, i));
143 list->clear(list);
144 for (T *item : std::as_const(stash))
145 list->append(list, item);
146 } else {
147 stash.reserve(length - idx - 1);
148 for (qsizetype i = length - 1; i > idx; --i) {
149 stash.append(list->at(list, i));
151 }
153 list->append(list, v);
154 while (!stash.isEmpty())
155 list->append(list, stash.takeLast());
156 }
157 }
158
159 static void qslow_clear(QQmlListProperty<T> *list)
160 {
161 for (qsizetype i = 0, end = list->count(list); i < end; ++i)
163 }
164
165 static void qslow_removeLast(QQmlListProperty<T> *list)
166 {
167 const qsizetype length = list->count(list) - 1;
168 if (length < 0)
169 return;
170 QVector<T *> stash;
171 stash.reserve(length);
172 for (qsizetype i = 0; i < length; ++i)
173 stash.append(list->at(list, i));
174 list->clear(list);
175 for (T *item : std::as_const(stash))
176 list->append(list, item);
177 }
178};
179
180class QQmlEngine;
182class Q_QML_EXPORT QQmlListReference
183{
184public:
186
187#if QT_DEPRECATED_SINCE(6, 4)
188 QT_DEPRECATED_X("Drop the QQmlEngine* argument")
189 QQmlListReference(const QVariant &variant, [[maybe_unused]] QQmlEngine *engine);
190
191 QT_DEPRECATED_X("Drop the QQmlEngine* argument")
192 QQmlListReference(QObject *o, const char *property, [[maybe_unused]] QQmlEngine *engine);
193#endif
194
195 explicit QQmlListReference(const QVariant &variant);
196 QQmlListReference(QObject *o, const char *property);
198 QQmlListReference &operator=(const QQmlListReference &);
200
201 bool isValid() const;
202
203 QObject *object() const;
204 const QMetaObject *listElementType() const;
205
206 bool canAppend() const;
207 bool canAt() const;
208 bool canClear() const;
209 bool canCount() const;
210 bool canReplace() const;
211 bool canRemoveLast() const;
212
213 bool isManipulable() const;
214 bool isReadable() const;
215
216 bool append(QObject *) const;
217 QObject *at(qsizetype) const;
218 bool clear() const;
219 qsizetype count() const;
220 qsizetype size() const { return count(); }
221 bool replace(qsizetype, QObject *) const;
222 bool removeLast() const;
223 bool operator==(const QQmlListReference &other) const {return d == other.d;}
224
225private:
228};
229
230namespace QtPrivate {
231template<typename T>
232inline constexpr bool IsQmlListType<QQmlListProperty<T>> = true;
233}
234
236
238
239#endif // QQMLLIST_H
const_reference at(qsizetype i) const noexcept
Definition qlist.h:446
value_type takeLast()
Definition qlist.h:567
qsizetype count() const noexcept
Definition qlist.h:398
void removeLast() noexcept
Definition qlist.h:815
void append(parameter_type t)
Definition qlist.h:458
void clear()
Definition qlist.h:434
\inmodule QtCore
Definition qobject.h:103
The QQmlEngine class provides an environment for instantiating QML components.
Definition qqmlengine.h:57
The QQmlListProperty class allows applications to expose list-like properties of QObject-derived clas...
Definition qqmllist.h:24
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:42
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:54
ClearFunction clear
Definition qqmllist.h:87
void(*)(QQmlListProperty< T > *, qsizetype, T *) ReplaceFunction
Synonym for {void (*)(QQmlListProperty<T> *property, qsizetype index, T *value)}.
Definition qqmllist.h:32
QQmlListProperty(QObject *o, QList< T * > *list)
Definition qqmllist.h:37
bool operator==(const QQmlListProperty &o) const
Returns true if this QQmlListProperty is equal to other, otherwise false.
Definition qqmllist.h:70
QQmlListProperty()=default
\macro QML_LIST_PROPERTY_ASSIGN_BEHAVIOR_APPEND
AtFunction at
Definition qqmllist.h:86
T *(*)(QQmlListProperty< T > *, qsizetype) AtFunction
Synonym for {T *(*)(QQmlListProperty<T> *property, qsizetype index)}.
Definition qqmllist.h:30
qsizetype(*)(QQmlListProperty< T > *) CountFunction
Synonym for {qsizetype (*)(QQmlListProperty<T> *property)}.
Definition qqmllist.h:29
RemoveLastFunction removeLast
Definition qqmllist.h:89
void(*)(QQmlListProperty< T > *) RemoveLastFunction
Synonym for {void (*)(QQmlListProperty<T> *property)}.
Definition qqmllist.h:33
CountFunction count
Definition qqmllist.h:85
void(*)(QQmlListProperty< T > *, T *) AppendFunction
Synonym for {void (*)(QQmlListProperty<T> *property, T *value)}.
Definition qqmllist.h:28
AppendFunction append
Definition qqmllist.h:84
void(*)(QQmlListProperty< T > *) ClearFunction
Synonym for {void (*)(QQmlListProperty<T> *property)}.
Definition qqmllist.h:31
ReplaceFunction replace
Definition qqmllist.h:88
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:66
The QQmlListReference class allows the manipulation of QQmlListProperty properties.
Definition qqmllist.h:183
bool operator==(const QQmlListReference &other) const
Compares this QQmlListReference to other, and returns true if they are equal.
Definition qqmllist.h:223
qsizetype size() const
Definition qqmllist.h:220
\inmodule QtCore
Definition qvariant.h:65
b clear()
list append(new Employee("Blackpool", "Stephen"))
Combined button and popup list for selecting options.
\macro QT_NO_KEYWORDS >
DBusConnection const char DBusError DBusBusType DBusError return DBusConnection DBusHandleMessageFunction void DBusFreeFunction return DBusConnection return DBusConnection return const char DBusError return DBusConnection DBusMessage dbus_uint32_t return DBusConnection dbus_bool_t DBusConnection DBusAddWatchFunction DBusRemoveWatchFunction DBusWatchToggledFunction void DBusFreeFunction return DBusConnection DBusDispatchStatusFunction void DBusFreeFunction DBusTimeout return DBusTimeout return DBusWatch return DBusWatch unsigned int return DBusError const DBusError return const DBusMessage return DBusMessage return DBusMessage return DBusMessage return DBusMessage return DBusMessage return DBusMessageIter int const void return DBusMessageIter DBusMessageIter return DBusMessageIter void DBusMessageIter void int return DBusMessage DBusMessageIter return DBusMessageIter return DBusMessageIter DBusMessageIter const char const char const char const char return DBusMessage return DBusMessage const char return DBusMessage dbus_bool_t return DBusMessage dbus_uint32_t return DBusMessage void
#define Q_DECLARE_METATYPE(TYPE)
Definition qmetatype.h:1525
GLsizei const GLfloat * v
[13]
GLboolean GLboolean GLboolean GLboolean a
[7]
GLenum GLuint GLintptr GLsizeiptr size
[1]
GLboolean r
[2]
GLuint GLuint end
GLenum GLuint GLenum GLsizei length
GLenum GLenum GLsizei count
GLuint object
[3]
GLint GLsizei GLsizei GLenum GLenum GLsizei void * data
GLdouble s
[6]
Definition qopenglext.h:235
const GLubyte * c
GLdouble GLdouble t
Definition qopenglext.h:243
GLuint64EXT * result
[6]
GLfloat GLfloat p
[1]
#define QT_DEPRECATED_X(text)
return device isReadable() ? device -> read(static_cast< char * >(buf), size) :-1
ptrdiff_t qsizetype
Definition qtypes.h:165
const char property[13]
Definition qwizard.cpp:101
QList< int > list
[14]
QObject::connect nullptr
QVariant variant
[1]
QSharedPointer< T > other(t)
[5]
QGraphicsItem * item
QAction * at
QJSEngine engine
[0]
\inmodule QtCore