aboutsummaryrefslogtreecommitdiffstats
path: root/src/qml/qml/qqmlvaluetypewrapper.cpp
diff options
context:
space:
mode:
authorGabriel de Dietrich <gabriel.dedietrich@theqtcompany.com>2015-01-14 20:54:13 +0100
committerGabriel de Dietrich <gabriel.dedietrich@theqtcompany.com>2015-01-20 14:37:40 +0100
commit3b834f53049ff54334c87ca5d00f0644baeddb3a (patch)
tree5fcb79373d0a41e5b67737c1001a02cecc57f033 /src/qml/qml/qqmlvaluetypewrapper.cpp
parent606ce564d82dd099fb1fca939692839cf571b1ac (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 'src/qml/qml/qqmlvaluetypewrapper.cpp')
-rw-r--r--src/qml/qml/qqmlvaluetypewrapper.cpp28
1 files changed, 18 insertions, 10 deletions
diff --git a/src/qml/qml/qqmlvaluetypewrapper.cpp b/src/qml/qml/qqmlvaluetypewrapper.cpp
index b72b89c11d..76be5ae895 100644
--- a/src/qml/qml/qqmlvaluetypewrapper.cpp
+++ b/src/qml/qml/qqmlvaluetypewrapper.cpp
@@ -263,17 +263,25 @@ ReturnedValue QQmlValueTypeWrapper::method_toString(CallContext *ctx)
if (!ref->readReferenceValue())
return Encode::undefined();
- QString result = QString::fromUtf8(QMetaType::typeName(w->d()->metaType));
- result += QLatin1Char('(');
- const QMetaObject *mo = w->d()->propertyCache->metaObject();
- const int propCount = mo->propertyCount();
- for (int i = 0; i < propCount; ++i) {
- QVariant value = mo->property(i).readOnGadget(w->d()->gadget());
- result += value.toString();
- if (i < propCount - 1)
- result += QStringLiteral(", ");
+ QString result;
+ // Prepare a buffer to pass to QMetaType::convert()
+ QString convertResult;
+ convertResult.~QString();
+ if (QMetaType::convert(w->d()->gadgetPtr, w->d()->metaType, &convertResult, QMetaType::QString)) {
+ result = convertResult;
+ } else {
+ result = QString::fromUtf8(QMetaType::typeName(w->d()->metaType));
+ result += QLatin1Char('(');
+ const QMetaObject *mo = w->d()->propertyCache->metaObject();
+ const int propCount = mo->propertyCount();
+ for (int i = 0; i < propCount; ++i) {
+ QVariant value = mo->property(i).readOnGadget(w->d()->gadget());
+ result += value.toString();
+ if (i < propCount - 1)
+ result += QStringLiteral(", ");
+ }
+ result += QLatin1Char(')');
}
- result += QLatin1Char(')');
return Encode(ctx->engine()->newString(result));
}