aboutsummaryrefslogtreecommitdiffstats
path: root/src/qml/util
diff options
context:
space:
mode:
authorUlf Hermann <ulf.hermann@qt.io>2021-01-13 11:50:07 +0100
committerUlf Hermann <ulf.hermann@qt.io>2021-01-13 14:32:11 +0100
commitb64f8dacae36fca948933cf56498d5e4ad3e2a07 (patch)
tree3c41c0270d6b4ab13b104581b0f1024e0e3cddab /src/qml/util
parent315261a809778a8ac37c523741e021d6431ab85e (diff)
QQmlPropertyMap: Add a method to insert multiple values at once
This avoid re-building the metaobject for every property added. As rebuilding the metaobject is an effort linear in the number of properties, the runtime when adding multiple properties via singular insert() is quadratic in the number of properties. The plural insert() rebuilds the metaobject only once. Task-number: QTBUG-57792 Change-Id: I9513c4de047724e4141dab72aacfbdd840a3e465 Reviewed-by: Fabian Kosmale <fabian.kosmale@qt.io>
Diffstat (limited to 'src/qml/util')
-rw-r--r--src/qml/util/qqmlpropertymap.cpp30
-rw-r--r--src/qml/util/qqmlpropertymap.h1
2 files changed, 31 insertions, 0 deletions
diff --git a/src/qml/util/qqmlpropertymap.cpp b/src/qml/util/qqmlpropertymap.cpp
index e5fa66aded..e38cf3a2a9 100644
--- a/src/qml/util/qqmlpropertymap.cpp
+++ b/src/qml/util/qqmlpropertymap.cpp
@@ -234,6 +234,36 @@ void QQmlPropertyMap::insert(const QString &key, const QVariant &value)
}
/*!
+ \since 6.1
+
+ Inserts the \a values into the QQmlPropertyMap.
+
+ Keys that don't exist are automatically created.
+
+ This method is substantially faster than calling \c{insert(key, value)}
+ many times in a row.
+*/
+void QQmlPropertyMap::insert(const QVariantHash &values)
+{
+ Q_D(QQmlPropertyMap);
+
+ QHash<QByteArray, QVariant> checkedValues;
+ for (auto it = values.begin(), end = values.end(); it != end; ++it) {
+ const QString &key = it.key();
+ if (!d->validKeyName(key)) {
+ qWarning() << "Creating property with name"
+ << key
+ << "is not permitted, conflicts with internal symbols.";
+ return;
+ }
+
+ checkedValues.insert(key.toUtf8(), it.value());
+ }
+ d->mo->setValues(checkedValues);
+
+}
+
+/*!
Returns the list of keys.
Keys that have been cleared will still appear in this list, even though their
diff --git a/src/qml/util/qqmlpropertymap.h b/src/qml/util/qqmlpropertymap.h
index d948989833..556754c021 100644
--- a/src/qml/util/qqmlpropertymap.h
+++ b/src/qml/util/qqmlpropertymap.h
@@ -60,6 +60,7 @@ public:
QVariant value(const QString &key) const;
void insert(const QString &key, const QVariant &value);
+ void insert(const QVariantHash &values);
void clear(const QString &key);
Q_INVOKABLE QStringList keys() const;