aboutsummaryrefslogtreecommitdiffstats
path: root/src/qml
diff options
context:
space:
mode:
authorSimon Hausmann <simon.hausmann@digia.com>2014-04-11 12:16:44 +0200
committerThe Qt Project <gerrit-noreply@qt-project.org>2014-04-12 08:03:16 +0200
commitff0857541d5d391c7c03cce5893b41dd9b35e7fa (patch)
tree0d20dba80e8aa3e541a7d51b135c8a6b65b60019 /src/qml
parentde11a78c625b0865f8cd3d0cfe446cba2bdb719e (diff)
Fix assignments of null strings in QML and JavaScript
Assigning "" to a string based property would exhibit inconsistent behavior: * When assigned as a literal, it would assign an empty string * When assigned from JavaScript, it would assign a null string When the assignment was done _to_ a QByteArray property, it would hit the case of calling QVariant::convert where the incoming variant is either an empty or a null string and the target is a QByteArray. For historical reasons - as documented - QVariant::convert will return false when the incoming variant is a null variant. In V8 assignment from JavaScript would produce an empty string and thus hit the "succesfull" conversion code path in QVariant to convert to a seemingly empty QByteArray. With v4 a null string would result in a failed conversion and spurious warnings as seen in the reported task. This patch ensures that we consistently map "" to a null string, when it comes from JavaScript or QML as a literal string. We now also detect the situation of trying to convert a (valid) null variant to another target type. Task-number: QTBUG-37197 Change-Id: I68f9031262fdd287d69a38d5468fb38a20441d7b Reviewed-by: Jędrzej Nowacki <jedrzej.nowacki@digia.com>
Diffstat (limited to 'src/qml')
-rw-r--r--src/qml/compiler/qv4compileddata_p.h2
-rw-r--r--src/qml/qml/qqmlproperty.cpp6
2 files changed, 8 insertions, 0 deletions
diff --git a/src/qml/compiler/qv4compileddata_p.h b/src/qml/compiler/qv4compileddata_p.h
index afcf2c6a24..a4dcfd9209 100644
--- a/src/qml/compiler/qv4compileddata_p.h
+++ b/src/qml/compiler/qv4compileddata_p.h
@@ -196,6 +196,8 @@ struct Unit
const uint *offsetTable = reinterpret_cast<const uint*>((reinterpret_cast<const char *>(this)) + offsetToStringTable);
const uint offset = offsetTable[idx];
const String *str = reinterpret_cast<const String*>(reinterpret_cast<const char *>(this) + offset);
+ if (str->str.size == 0)
+ return QString();
QStringDataPtr holder = { const_cast<QStringData *>(static_cast<const QStringData*>(&str->str)) };
QString qstr(holder);
if (flags & StaticData)
diff --git a/src/qml/qml/qqmlproperty.cpp b/src/qml/qml/qqmlproperty.cpp
index 0bbcafda54..1075b53c5e 100644
--- a/src/qml/qml/qqmlproperty.cpp
+++ b/src/qml/qml/qqmlproperty.cpp
@@ -1403,6 +1403,12 @@ bool QQmlPropertyPrivate::write(QObject *object,
v = value;
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
+ // but return false anyway. This is caught with the above condition and considered a
+ // successful conversion.
+ Q_ASSERT(v.userType() == propertyType);
+ ok = true;
} else if ((uint)propertyType >= QVariant::UserType && variantType == QVariant::String) {
QQmlMetaType::StringConverter con = QQmlMetaType::customStringConverter(propertyType);
if (con) {