diff options
author | Matthew Vogt <matthew.vogt@nokia.com> | 2012-07-17 11:14:43 +1000 |
---|---|---|
committer | Qt by Nokia <qt-info@nokia.com> | 2012-07-18 05:48:43 +0200 |
commit | 884bc89e9fe765a7be245b3009339f999936a761 (patch) | |
tree | a8ecc8df740a3e93a8ab30de96f7a4e4a2e091e9 /tests/auto | |
parent | 1ce3a17ef51e10c03627842b9cdd0bb543ee8c83 (diff) |
Allow QQmlPropertyMap property updates to be controlled
Allow clients to control updates made from QML to types derived from
QQmlPropertyMap, by overriding the updateValue() function.
Task-number: QTBUG-23183
Change-Id: I0169093779ebfe50dc9349f5aaac08ed85c80a8f
Reviewed-by: Michael Brasser <michael.brasser@nokia.com>
Reviewed-by: abcd <amos.choy@nokia.com>
Diffstat (limited to 'tests/auto')
-rw-r--r-- | tests/auto/qml/qqmlpropertymap/tst_qqmlpropertymap.cpp | 44 |
1 files changed, 44 insertions, 0 deletions
diff --git a/tests/auto/qml/qqmlpropertymap/tst_qqmlpropertymap.cpp b/tests/auto/qml/qqmlpropertymap/tst_qqmlpropertymap.cpp index 0ae05ce138..4e39ad3a41 100644 --- a/tests/auto/qml/qqmlpropertymap/tst_qqmlpropertymap.cpp +++ b/tests/auto/qml/qqmlpropertymap/tst_qqmlpropertymap.cpp @@ -45,6 +45,7 @@ #include <QtQml/qqmlcomponent.h> #include <QtQuick/private/qquicktext_p.h> #include <QSignalSpy> +#include <QDebug> class tst_QQmlPropertyMap : public QObject { @@ -59,6 +60,7 @@ private slots: void clear(); void changed(); void count(); + void controlledWrite(); void crashBug(); void QTBUG_17868(); @@ -205,6 +207,48 @@ void tst_QQmlPropertyMap::count() QCOMPARE(map.size(), map.count()); } +class MyPropertyMap : public QQmlPropertyMap +{ + Q_OBJECT +protected: + virtual QVariant updateValue(const QString &key, const QVariant &src) + { + if (key == QLatin1String("key1")) { + // 'key1' must be all uppercase + const QString original(src.toString()); + return QVariant(original.toUpper()); + } + + return src; + } +}; + +void tst_QQmlPropertyMap::controlledWrite() +{ + MyPropertyMap map; + QCOMPARE(map.isEmpty(), true); + + //make changes in QML + QQmlEngine engine; + QQmlContext *ctxt = engine.rootContext(); + ctxt->setContextProperty(QLatin1String("testdata"), &map); + + const char *qmlSource = + "import QtQuick 2.0\n" + "Item { Component.onCompleted: { testdata.key1 = 'Hello World'; testdata.key2 = 'Goodbye' } }"; + + QQmlComponent component(&engine); + component.setData(qmlSource, QUrl::fromLocalFile("")); + QVERIFY(component.isReady()); + + QObject *obj = component.create(); + QVERIFY(obj); + delete obj; + + QCOMPARE(map.value(QLatin1String("key1")), QVariant("HELLO WORLD")); + QCOMPARE(map.value(QLatin1String("key2")), QVariant("Goodbye")); +} + void tst_QQmlPropertyMap::crashBug() { QQmlPropertyMap map; |