aboutsummaryrefslogtreecommitdiffstats
path: root/tests/auto/qml/qqmlproperty
diff options
context:
space:
mode:
authorChristian Strømme <christian.stromme@theqtcompany.com>2015-03-12 15:33:50 +0100
committerChristian Stromme <christian.stromme@theqtcompany.com>2015-03-21 11:15:31 +0000
commit90b06e27738423b8012d1c16672b60a971fe8b4c (patch)
tree4d3d26fabcd191a6404712358b2ebd9bce4032fd /tests/auto/qml/qqmlproperty
parenta86302d34f473811608f49643e54975425493628 (diff)
Fix conversion between char and string.
If a QChar (or char) was used to set a QString property, the intermediate value used by the QML engine (int), would be converted to a string representation of the integer and not the actual character. To avoid this behavior, characters are now stored as string objects and the string is then converted to the target char type if possible. A side effect of this solution is that it is makes it possible to assign a string to a char property as well, but only if the string contains exactly one character. [ChangeLog][QtQml][Important Behavior Changes] Assigning a char to a string will now create a string with the actual character instead of a string representation of the character's code-point. A side effect of this change is that a one-character string also can be assigned to a character type. Task-number: QTBUG-44934 Change-Id: Ifd15386933ee11354ee1bbb5598a5f0b00a08616 Reviewed-by: Alan Alpert (Personal) <416365416c@gmail.com> Reviewed-by: Simon Hausmann <simon.hausmann@theqtcompany.com>
Diffstat (limited to 'tests/auto/qml/qqmlproperty')
-rw-r--r--tests/auto/qml/qqmlproperty/tst_qqmlproperty.cpp88
1 files changed, 87 insertions, 1 deletions
diff --git a/tests/auto/qml/qqmlproperty/tst_qqmlproperty.cpp b/tests/auto/qml/qqmlproperty/tst_qqmlproperty.cpp
index f8af13582e..c4b2325843 100644
--- a/tests/auto/qml/qqmlproperty/tst_qqmlproperty.cpp
+++ b/tests/auto/qml/qqmlproperty/tst_qqmlproperty.cpp
@@ -326,10 +326,16 @@ class PropertyObject : public QObject
Q_PROPERTY(int propertyWithNotify READ propertyWithNotify WRITE setPropertyWithNotify NOTIFY oddlyNamedNotifySignal)
Q_PROPERTY(MyQmlObject *qmlObject READ qmlObject)
Q_PROPERTY(MyQObject *qObject READ qObject WRITE setQObject NOTIFY qObjectChanged)
+ Q_PROPERTY(QString stringProperty READ stringProperty WRITE setStringProperty)
+ Q_PROPERTY(char charProperty READ charProperty WRITE setCharProperty)
+ Q_PROPERTY(QChar qcharProperty READ qcharProperty WRITE setQcharProperty)
+ Q_PROPERTY(QChar constQChar READ constQChar STORED false CONSTANT FINAL)
+ Q_PROPERTY(char constChar READ constChar STORED false CONSTANT FINAL)
+ Q_PROPERTY(int constInt READ constInt STORED false CONSTANT FINAL)
Q_CLASSINFO("DefaultProperty", "defaultProperty")
public:
- PropertyObject() : m_resetProperty(9), m_qObject(0) {}
+ PropertyObject() : m_resetProperty(9), m_qObject(0), m_stringProperty("foo") {}
int defaultProperty() { return 10; }
QRect rectProperty() { return QRect(10, 10, 1, 209); }
@@ -361,6 +367,18 @@ public:
}
}
+ QString stringProperty() const { return m_stringProperty;}
+ char charProperty() const { return m_charProperty; }
+ QChar qcharProperty() const { return m_qcharProperty; }
+
+ QChar constQChar() const { return 0x25cf; /* Unicode: black circle */ }
+ char constChar() const { return 'A'; }
+ int constInt() const { return 123456; }
+
+ void setStringProperty(QString arg) { m_stringProperty = arg; }
+ void setCharProperty(char arg) { m_charProperty = arg; }
+ void setQcharProperty(QChar arg) { m_qcharProperty = arg; }
+
signals:
void clicked();
void oddlyNamedNotifySignal();
@@ -374,6 +392,9 @@ private:
int m_propertyWithNotify;
MyQmlObject m_qmlObject;
MyQObject *m_qObject;
+ QString m_stringProperty;
+ char m_charProperty;
+ QChar m_qcharProperty;
};
QML_DECLARE_TYPE(PropertyObject);
@@ -1382,6 +1403,71 @@ void tst_qqmlproperty::write()
QCOMPARE(o.url(), result);
}
+ // Char/string-property
+ {
+ PropertyObject o;
+ QQmlProperty charProperty(&o, "charProperty");
+ QQmlProperty qcharProperty(&o, "qcharProperty");
+ QQmlProperty stringProperty(&o, "stringProperty");
+
+ const int black_circle = 0x25cf;
+
+ QCOMPARE(charProperty.write(QString("foo")), false);
+ QCOMPARE(charProperty.write('Q'), true);
+ QCOMPARE(charProperty.read(), QVariant('Q'));
+ QCOMPARE(charProperty.write(QString("t")), true);
+ QCOMPARE(charProperty.read(), QVariant('t'));
+
+ QCOMPARE(qcharProperty.write(QString("foo")), false);
+ QCOMPARE(qcharProperty.write('Q'), true);
+ QCOMPARE(qcharProperty.read(), QVariant('Q'));
+ QCOMPARE(qcharProperty.write(QString("t")), true);
+ QCOMPARE(qcharProperty.read(), QVariant('t'));
+ QCOMPARE(qcharProperty.write(QChar(black_circle)), true);
+ QCOMPARE(qcharProperty.read(), QVariant(QChar(black_circle)));
+
+ QCOMPARE(o.stringProperty(), QString("foo")); // Default value
+ QCOMPARE(stringProperty.write(QString("bar")), true);
+ QCOMPARE(o.stringProperty(), QString("bar"));
+ QCOMPARE(stringProperty.write(QVariant(1234)), true);
+ QCOMPARE(stringProperty.read().toString(), QString::number(1234));
+ QCOMPARE(stringProperty.write(QChar(black_circle)), true);
+ QCOMPARE(stringProperty.read(), QVariant(QString(QChar(black_circle))));
+
+ { // char -> QString
+ QQmlComponent component(&engine);
+ component.setData("import Test 1.0\nPropertyObject { stringProperty: constChar }", QUrl());
+ PropertyObject *obj = qobject_cast<PropertyObject*>(component.create());
+ QVERIFY(obj != 0);
+ if (obj) {
+ QQmlProperty stringProperty(obj, "stringProperty");
+ QCOMPARE(stringProperty.read(), QVariant(QString(obj->constChar())));
+ }
+ }
+
+ { // QChar -> QString
+ QQmlComponent component(&engine);
+ component.setData("import Test 1.0\nPropertyObject { stringProperty: constQChar }", QUrl());
+ PropertyObject *obj = qobject_cast<PropertyObject*>(component.create());
+ QVERIFY(obj != 0);
+ if (obj) {
+ QQmlProperty stringProperty(obj, "stringProperty");
+ QCOMPARE(stringProperty.read(), QVariant(QString(obj->constQChar())));
+ }
+ }
+
+ { // int -> QString
+ QQmlComponent component(&engine);
+ component.setData("import Test 1.0\nPropertyObject { stringProperty: constInt }", QUrl());
+ PropertyObject *obj = qobject_cast<PropertyObject*>(component.create());
+ QVERIFY(obj != 0);
+ if (obj) {
+ QQmlProperty stringProperty(obj, "stringProperty");
+ QCOMPARE(stringProperty.read(), QVariant(QString::number(obj->constInt())));
+ }
+ }
+ }
+
// VariantMap-property
QVariantMap vm;
vm.insert("key", "value");