diff options
author | Ulf Hermann <ulf.hermann@theqtcompany.com> | 2015-09-21 13:27:46 +0200 |
---|---|---|
committer | Ulf Hermann <ulf.hermann@theqtcompany.com> | 2015-09-23 07:46:35 +0000 |
commit | a9276971aa8018837b23a847b5dee3b63b39232e (patch) | |
tree | 3b61379600a2ea165713fd6663fa3c8efa37083e | |
parent | d0025a064cc36b4076a33fbda2c6b48afecfd13b (diff) |
Use V4 double-to-string conversion instead of QVariant's
V4's version makes an effort to find the shortest possible
representation, which QVariant doesn't do.
Task-number: QTBUG-47070
Change-Id: I49ce130020496592325074e0db29a6984ee7649a
Reviewed-by: Simon Hausmann <simon.hausmann@theqtcompany.com>
-rw-r--r-- | src/qml/qml/qqmlproperty.cpp | 8 | ||||
-rw-r--r-- | tests/auto/qml/qqmlproperty/data/floatToStringPrecision.qml | 10 | ||||
-rw-r--r-- | tests/auto/qml/qqmlproperty/tst_qqmlproperty.cpp | 22 |
3 files changed, 39 insertions, 1 deletions
diff --git a/src/qml/qml/qqmlproperty.cpp b/src/qml/qml/qqmlproperty.cpp index c1120b4542..d3f7070528 100644 --- a/src/qml/qml/qqmlproperty.cpp +++ b/src/qml/qml/qqmlproperty.cpp @@ -50,6 +50,7 @@ #include "qqmlvaluetypeproxybinding_p.h" #include <private/qjsvalue_p.h> #include <private/qv4functionobject_p.h> +#include <private/qv4runtime_p.h> #include <QStringList> #include <private/qmetaobject_p.h> @@ -1350,7 +1351,12 @@ bool QQmlPropertyPrivate::write(QObject *object, if (!ok) { v = value; - if (v.convert(propertyType)) { + if (variantType == QVariant::Double && propertyType == QVariant::String) { + QString number; + QV4::RuntimeHelpers::numberToString(&number, v.toDouble()); + v = number; + ok = true; + } else if (v.convert(propertyType)) { ok = true; } else if (v.isValid() && value.isNull()) { // For historical reasons converting a null QVariant to another type will do the trick diff --git a/tests/auto/qml/qqmlproperty/data/floatToStringPrecision.qml b/tests/auto/qml/qqmlproperty/data/floatToStringPrecision.qml new file mode 100644 index 0000000000..a0429e0cc8 --- /dev/null +++ b/tests/auto/qml/qqmlproperty/data/floatToStringPrecision.qml @@ -0,0 +1,10 @@ +import QtQuick 2.0 + +QtObject { + property double a: 3.4 + property string b: a + + property double c: 0.035003945 + property string d: c +} + diff --git a/tests/auto/qml/qqmlproperty/tst_qqmlproperty.cpp b/tests/auto/qml/qqmlproperty/tst_qqmlproperty.cpp index d6b1c86b88..6ada14ce79 100644 --- a/tests/auto/qml/qqmlproperty/tst_qqmlproperty.cpp +++ b/tests/auto/qml/qqmlproperty/tst_qqmlproperty.cpp @@ -147,6 +147,7 @@ private slots: void registeredCompositeTypeProperty(); void deeplyNestedObject(); void readOnlyDynamicProperties(); + void floatToStringPrecision(); void copy(); private: @@ -2054,6 +2055,27 @@ void tst_qqmlproperty::readOnlyDynamicProperties() delete obj; } +void tst_qqmlproperty::floatToStringPrecision() +{ + QQmlComponent comp(&engine, testFileUrl("floatToStringPrecision.qml")); + QObject *obj = comp.create(); + QVERIFY(obj != 0); + + QCOMPARE(obj->property("a").toDouble(), 3.4); + QEXPECT_FAIL("", "QVariant's double-to-string conversion is worse than V4's.", Continue); + QCOMPARE(obj->property("a").toString(), QLatin1String("3.4")); + QCOMPARE(obj->property("b").toDouble(), 3.4); + QCOMPARE(obj->property("b").toString(), QLatin1String("3.4")); + + QCOMPARE(obj->property("c").toDouble(), 0.035003945); + QEXPECT_FAIL("", "QVariant's double-to-string conversion is worse than V4's.", Continue); + QCOMPARE(obj->property("c").toString(), QLatin1String("0.035003945")); + QCOMPARE(obj->property("d").toDouble(), 0.035003945); + QCOMPARE(obj->property("d").toString(), QLatin1String("0.035003945")); + + delete obj; +} + void tst_qqmlproperty::initTestCase() { QQmlDataTest::initTestCase(); |