diff options
author | Thomas Hartmann <thomas.hartmann@qt.io> | 2018-01-10 15:59:07 +0100 |
---|---|---|
committer | Thomas Hartmann <thomas.hartmann@qt.io> | 2018-01-12 07:03:29 +0000 |
commit | 4d6830546619d16275b01f1f049fdcb0b6489f7a (patch) | |
tree | feee3a660e22affd2ad4f6d5858478edecdac4df /src | |
parent | 6318a43ffc473ba101c955d96b39bc1b93ef7e61 (diff) |
Add setContextProperties()
Setting all properties in one batch avoids unnecessary
refreshing of expressions and is therefore a lot
faster if there are many expressions in the context.
In an example I created it takes 500ms to set 10 context properties
using setContextProperty() and it takes about 150ms to set the
context properties using setContextProperties.
Change-Id: Ic7cf03cda292b316198f37f963b61a2388a288c9
Reviewed-by: Thomas Hartmann <thomas.hartmann@qt.io>
Reviewed-by: Simon Hausmann <simon.hausmann@qt.io>
Diffstat (limited to 'src')
-rw-r--r-- | src/qml/qml/qqmlcontext.cpp | 43 | ||||
-rw-r--r-- | src/qml/qml/qqmlcontext.h | 5 |
2 files changed, 48 insertions, 0 deletions
diff --git a/src/qml/qml/qqmlcontext.cpp b/src/qml/qml/qqmlcontext.cpp index a32ed6e998..9d2e37a2ab 100644 --- a/src/qml/qml/qqmlcontext.cpp +++ b/src/qml/qml/qqmlcontext.cpp @@ -330,6 +330,49 @@ void QQmlContext::setContextProperty(const QString &name, QObject *value) } /*! + \since 5.11 + + Set a batch of \a properties on this context. + + Setting all properties in one batch avoids unnecessary + refreshing expressions, and is therefore recommended + instead of calling \l setContextProperty() for each individual property. + + \sa QQmlContext::setContextProperty() +*/ +void QQmlContext::setContextProperties(const QVector<PropertyPair> &properties) +{ + Q_D(const QQmlContext); + + QQmlContextData *data = d->data; + + QQmlJavaScriptExpression *expressions = data->expressions; + QQmlContextData *childContexts = data->childContexts; + + data->expressions = 0; + data->childContexts = 0; + + for (auto property : properties) + setContextProperty(property.name, property.value); + + data->expressions = expressions; + data->childContexts = childContexts; + + data->refreshExpressions(); +} + +/*! + \since 5.11 + + \class QQmlContext::PropertyPair + + This struct contains a property name and a property value. + It is used as a parameter for the \c setContextProperties function. + + \sa QQQmlContext::setContextProperties() +*/ + +/*! Returns the value of the \a name property for this context as a QVariant. */ diff --git a/src/qml/qml/qqmlcontext.h b/src/qml/qml/qqmlcontext.h index b2b95b7573..506ae216b2 100644 --- a/src/qml/qml/qqmlcontext.h +++ b/src/qml/qml/qqmlcontext.h @@ -42,6 +42,8 @@ #include <QtCore/qurl.h> #include <QtCore/qobject.h> +#include <QtCore/qlist.h> +#include <QtCore/qpair.h> #include <QtQml/qjsvalue.h> #include <QtCore/qmetatype.h> #include <QtCore/qvariant.h> @@ -62,6 +64,8 @@ class Q_QML_EXPORT QQmlContext : public QObject Q_DECLARE_PRIVATE(QQmlContext) public: + struct PropertyPair { QString name; QVariant value; }; + QQmlContext(QQmlEngine *parent, QObject *objParent = nullptr); QQmlContext(QQmlContext *parent, QObject *objParent = nullptr); virtual ~QQmlContext(); @@ -77,6 +81,7 @@ public: QVariant contextProperty(const QString &) const; void setContextProperty(const QString &, QObject *); void setContextProperty(const QString &, const QVariant &); + void setContextProperties(const QVector<PropertyPair> &properties); // ### Qt 6: no need for a mutable object, this should become a const QObject pointer QString nameForObject(QObject *) const; |