diff options
author | Christian Strømme <christian.stromme@theqtcompany.com> | 2015-03-12 15:33:50 +0100 |
---|---|---|
committer | Christian Stromme <christian.stromme@theqtcompany.com> | 2015-03-21 11:15:31 +0000 |
commit | 90b06e27738423b8012d1c16672b60a971fe8b4c (patch) | |
tree | 4d3d26fabcd191a6404712358b2ebd9bce4032fd /src/qml/qml/qqmlproperty.cpp | |
parent | a86302d34f473811608f49643e54975425493628 (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 'src/qml/qml/qqmlproperty.cpp')
-rw-r--r-- | src/qml/qml/qqmlproperty.cpp | 26 |
1 files changed, 24 insertions, 2 deletions
diff --git a/src/qml/qml/qqmlproperty.cpp b/src/qml/qml/qqmlproperty.cpp index d45f3ac19b..931adb9a13 100644 --- a/src/qml/qml/qqmlproperty.cpp +++ b/src/qml/qml/qqmlproperty.cpp @@ -1387,8 +1387,30 @@ bool QQmlPropertyPrivate::write(QObject *object, bool ok = false; QVariant v; - if (variantType == QVariant::String) - v = QQmlStringConverters::variantFromString(value.toString(), propertyType, &ok); + if (variantType == QVariant::String) { + const QString &str = value.toString(); + const bool targetIsChar = (propertyType == qMetaTypeId<QChar>() + || propertyType == qMetaTypeId<char>() + || propertyType == qMetaTypeId<unsigned char>()); + // If the string contains only one character and the target is a char, try converting it. + if (targetIsChar) { + if (str.size() != 1) + return false; // We can only convert if the string contains exactly one character. + + const QChar &qChar = str.at(0); + if (propertyType == qMetaTypeId<QChar>()) { + v = qChar; + ok = true; + } else if (propertyType == qMetaTypeId<char>() || propertyType == qMetaTypeId<unsigned char>()) { + const char c = qChar.toLatin1(); + v = c; + ok = (qChar == c); + } + } else { + v = QQmlStringConverters::variantFromString(str, propertyType, &ok); + } + } + if (!ok) { v = value; if (v.convert(propertyType)) { |