diff options
author | Gabriel de Dietrich <gabriel.dedietrich@theqtcompany.com> | 2015-01-14 20:54:13 +0100 |
---|---|---|
committer | Gabriel de Dietrich <gabriel.dedietrich@theqtcompany.com> | 2015-01-20 14:37:40 +0100 |
commit | 3b834f53049ff54334c87ca5d00f0644baeddb3a (patch) | |
tree | 5fcb79373d0a41e5b67737c1001a02cecc57f033 /tests | |
parent | 606ce564d82dd099fb1fca939692839cf571b1ac (diff) |
QQmlValueTypeWrapper: Use QVariant to QString conversion if possible
This makes it easier to customize displaying Q_GADGETs that don't
have a toString() function defined. Instead of having to define
a wrapper Q_GADGET class, it's more straightforward to register a
meta-type converter. Finally, it ensures consistent value displays
between QML and the rest.
Change-Id: I76f93ee0bafabd74f311130972b49c572b38f43f
Reviewed-by: Simon Hausmann <simon.hausmann@digia.com>
Diffstat (limited to 'tests')
-rw-r--r-- | tests/auto/qml/qqmlvaluetypes/tst_qqmlvaluetypes.cpp | 30 |
1 files changed, 30 insertions, 0 deletions
diff --git a/tests/auto/qml/qqmlvaluetypes/tst_qqmlvaluetypes.cpp b/tests/auto/qml/qqmlvaluetypes/tst_qqmlvaluetypes.cpp index 45cbbebdc3..ac9ec37ab3 100644 --- a/tests/auto/qml/qqmlvaluetypes/tst_qqmlvaluetypes.cpp +++ b/tests/auto/qml/qqmlvaluetypes/tst_qqmlvaluetypes.cpp @@ -91,6 +91,7 @@ private slots: void customValueType(); void customValueTypeInQml(); void gadgetInheritance(); + void toStringConversion(); private: QQmlEngine engine; @@ -1559,6 +1560,35 @@ void tst_qqmlvaluetypes::gadgetInheritance() QCOMPARE(value.property("baseProperty").toInt(), 42); } +struct StringLessGadget { + Q_GADGET +}; + +Q_DECLARE_METATYPE(StringLessGadget) + +static QString StringLessGadget_to_QString(const StringLessGadget &) +{ + return QLatin1String("Surprise!"); +} + +void tst_qqmlvaluetypes::toStringConversion() +{ + QJSEngine engine; + + StringLessGadget g; + QJSValue value = engine.toScriptValue(g); + + QJSValue method = value.property("toString"); + QJSValue stringConversion = method.callWithInstance(value); + QCOMPARE(stringConversion.toString(), QString("StringLessGadget()")); + + QMetaType::registerConverter<StringLessGadget, QString>(StringLessGadget_to_QString); + + stringConversion = method.callWithInstance(value); + QCOMPARE(stringConversion.toString(), StringLessGadget_to_QString(g)); +} + + QTEST_MAIN(tst_qqmlvaluetypes) #include "tst_qqmlvaluetypes.moc" |