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
qqmlpropertymap.cpp
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#include "qqmlpropertymap.h"
5
6#include <private/qmetaobjectbuilder_p.h>
7#include <private/qqmlopenmetaobject_p.h>
8
9#include <QDebug>
10
12
13//QQmlPropertyMapMetaObject lets us listen for changes coming from QML
14//so we can emit the changed signal.
16{
17public:
19
20protected:
21 QVariant propertyWriteValue(int, const QVariant &) override;
22 void propertyWritten(int index) override;
23 void propertyCreated(int, QMetaPropertyBuilder &) override;
24
26
27private:
28 QQmlPropertyMap *map;
30};
31
33{
34 Q_DECLARE_PUBLIC(QQmlPropertyMap)
35public:
38
40 void emitChanged(const QString &key, const QVariant &value);
41 bool validKeyName(const QString& name);
42
43 const QString &propertyName(int index) const;
44};
45
47{
48 //The following strings shouldn't be used as property names
49 return name != QLatin1String("keys")
50 && name != QLatin1String("valueChanged")
51 && name != QLatin1String("QObject")
52 && name != QLatin1String("destroyed")
53 && name != QLatin1String("deleteLater");
54}
55
57{
58 Q_Q(QQmlPropertyMap);
59 return q->updateValue(key, input);
60}
61
63{
64 Q_Q(QQmlPropertyMap);
65 emit q->valueChanged(key, value);
66}
67
69{
70 Q_ASSERT(index < keys.size());
71 return keys[index];
72}
73
75 : QQmlOpenMetaObject(obj, staticMetaObject)
76{
77 map = obj;
78 priv = objPriv;
79}
80
85
90
95
149: QQmlPropertyMap(&staticMetaObject, parent)
150{
151}
152
159
164{
165 Q_D(QQmlPropertyMap);
166 if (d->validKeyName(key))
167 d->mo->setValue(key.toUtf8(), QVariant());
168}
169
180{
181 Q_D(QQmlPropertyMap);
182 d->mo->setAutoCreatesProperties(false);
183 d->mo->setCached(true);
184}
185
193{
194 Q_D(const QQmlPropertyMap);
195 return d->mo->value(key.toUtf8());
196}
197
204{
205 Q_D(QQmlPropertyMap);
206
207 if (d->validKeyName(key)) {
208 d->mo->setValue(key.toUtf8(), value);
209 } else {
210 qWarning() << "Creating property with name"
211 << key
212 << "is not permitted, conflicts with internal symbols.";
213 }
214}
215
227{
228 Q_D(QQmlPropertyMap);
229
230 QHash<QByteArray, QVariant> checkedValues;
231 for (auto it = values.begin(), end = values.end(); it != end; ++it) {
232 const QString &key = it.key();
233 if (!d->validKeyName(key)) {
234 qWarning() << "Creating property with name"
235 << key
236 << "is not permitted, conflicts with internal symbols.";
237 return;
238 }
239
240 checkedValues.insert(key.toUtf8(), it.value());
241 }
242 d->mo->setValues(checkedValues);
243
244}
245
253{
254 Q_D(const QQmlPropertyMap);
255 return d->keys;
256}
257
264{
265 Q_D(const QQmlPropertyMap);
266 return d->keys.size();
267}
268
275{
276 Q_D(const QQmlPropertyMap);
277 return d->keys.size();
278}
279
287{
288 Q_D(const QQmlPropertyMap);
289 return d->keys.isEmpty();
290}
291
298{
299 Q_D(const QQmlPropertyMap);
300 return d->keys.contains(key);
301}
302
314{
315 //### optimize
316 Q_D(QQmlPropertyMap);
317 QByteArray utf8key = key.toUtf8();
318 if (!d->keys.contains(key))
319 insert(key, QVariant());//force creation -- needed below
320
321 return d->mo->valueRef(utf8key);
322}
323
330{
331 return value(key);
332}
333
342{
343 Q_UNUSED(key);
344 return input;
345}
346
348QQmlPropertyMap::QQmlPropertyMap(const QMetaObject *staticMetaObject, QObject *parent)
349 : QObject(*(new QQmlPropertyMapPrivate), parent)
350{
351 Q_D(QQmlPropertyMap);
352 d->mo = new QQmlPropertyMapMetaObject(this, d, staticMetaObject);
353}
354
376
377#include "moc_qqmlpropertymap.cpp"
\inmodule QtCore
Definition qbytearray.h:57
\inmodule QtCore
Definition qobject.h:103
const QString & propertyName(int index)
void propertyWritten(int index) override
QVariant propertyWriteValue(int, const QVariant &) override
void propertyCreated(int, QMetaPropertyBuilder &) override
QQmlPropertyMapMetaObject(QQmlPropertyMap *obj, QQmlPropertyMapPrivate *objPriv, const QMetaObject *staticMetaObject)
QVariant updateValue(const QString &key, const QVariant &input)
void emitChanged(const QString &key, const QVariant &value)
const QString & propertyName(int index) const
QQmlPropertyMapMetaObject * mo
bool validKeyName(const QString &name)
The QQmlPropertyMap class allows you to set key-value pairs that can be used in QML bindings.
virtual QVariant updateValue(const QString &key, const QVariant &input)
Returns the new value to be stored for the key key.
QVariant & operator[](const QString &key)
Returns the value associated with the key key as a modifiable reference.
Q_INVOKABLE QStringList keys() const
Returns the list of keys.
void clear(const QString &key)
Clears the value (if any) associated with key.
int size() const
Returns the number of keys in the map.
bool contains(const QString &key) const
Returns true if the map contains key.
void insert(const QString &key, const QVariant &value)
Sets the value associated with key to value.
bool isEmpty() const
Returns true if the map contains no keys; otherwise returns false.
QVariant value(const QString &key) const
Returns the value associated with key.
~QQmlPropertyMap() override
Destroys the bindable map.
int count() const
This is an overloaded member function, provided for convenience. It differs from the above function o...
QQmlPropertyMap(QObject *parent=nullptr)
Constructs a bindable map with parent object parent.
iterator begin()
Definition qset.h:136
\inmodule QtCore
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:129
static QString fromUtf8(QByteArrayView utf8)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition qstring.cpp:6018
\inmodule QtCore
Definition qvariant.h:65
QSet< QString >::iterator it
Combined button and popup list for selecting options.
EGLOutputLayerEXT EGLint EGLAttrib value
[5]
#define qWarning
Definition qlogging.h:166
GLenum GLsizei GLsizei GLint * values
[15]
GLboolean GLboolean GLboolean b
GLuint64 key
GLuint index
[2]
GLuint GLuint end
GLuint name
GLhandleARB obj
[2]
GLdouble GLdouble GLdouble GLdouble q
Definition qopenglext.h:259
GLenum GLenum GLenum input
#define Q_ASSERT(cond)
Definition qrandom.cpp:47
QLatin1StringView QLatin1String
Definition qstringfwd.h:31
#define emit
#define Q_UNUSED(x)
\inmodule QtCore
static constexpr const QMetaObject * staticMetaObject()