aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorFabian Kosmale <fabian.kosmale@qt.io>2021-06-30 10:24:34 +0200
committerQt Cherry-pick Bot <cherrypick_bot@qt-project.org>2021-06-30 15:09:14 +0000
commit14e60183a97838814ceed7240e0f8ba9a2ec67f5 (patch)
tree5f674284c771e0fc23c85803878492db4e16f164
parent2fd54b5bef7dd2c8dd21ab7d2ff66145df1e4fa2 (diff)
ListModel: Fix accessing empty strings in ListElement
We store empty strings as null strings in StringOrTranslation; consequently isSet will return false for such a string. However, we still know that we are storing a string, and therefore need to return a QVariant containing an empty string, instead of a null QVariant. Amends 5022b91b31370e2afde0426f94ff2ce6843b92e6. This fixes the "undefined" warning in Calqlatr (but changes nothing about the fact that the example isn't reflective of modern QML practices). Task-number: QTBUG-54267 Change-Id: I83d871a79cd2bc0484f0eaf62d4ca345e39218aa Reviewed-by: Ulf Hermann <ulf.hermann@qt.io> (cherry picked from commit 9be6a9327af1ac96bccca087ef24100d89a5f8c5) Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
-rw-r--r--src/qmlmodels/qqmllistmodel.cpp2
-rw-r--r--tests/auto/qml/qqmllistmodel/tst_qqmllistmodel.cpp24
2 files changed, 26 insertions, 0 deletions
diff --git a/src/qmlmodels/qqmllistmodel.cpp b/src/qmlmodels/qqmllistmodel.cpp
index d983d7f88e..a9c58d59a5 100644
--- a/src/qmlmodels/qqmllistmodel.cpp
+++ b/src/qmlmodels/qqmllistmodel.cpp
@@ -941,6 +941,8 @@ QVariant ListElement::getProperty(const ListLayout::Role &role, const QQmlListMo
StringOrTranslation *value = reinterpret_cast<StringOrTranslation *>(mem);
if (value->isSet())
data = value->toString(owner);
+ else
+ data = QString();
}
break;
case ListLayout::Role::Bool:
diff --git a/tests/auto/qml/qqmllistmodel/tst_qqmllistmodel.cpp b/tests/auto/qml/qqmllistmodel/tst_qqmllistmodel.cpp
index e372db271e..150fb82ca6 100644
--- a/tests/auto/qml/qqmllistmodel/tst_qqmllistmodel.cpp
+++ b/tests/auto/qml/qqmllistmodel/tst_qqmllistmodel.cpp
@@ -134,6 +134,7 @@ private slots:
void nullPropertyCrash();
void objectDestroyed();
void destroyObject();
+ void emptyStringNotUndefined();
};
bool tst_qqmllistmodel::compareVariantList(const QVariantList &testList, QVariant object)
@@ -1815,6 +1816,29 @@ void tst_qqmllistmodel::destroyObject()
QCOMPARE(model->get(0).property("a").toQObject(), nullptr);
}
+void tst_qqmllistmodel::emptyStringNotUndefined()
+{
+ QQmlEngine engine;
+ QQmlComponent component(&engine);
+ component.setData(
+ R"(import QtQuick
+ ListModel {
+ id: model
+ Component.onCompleted: { model.append({"a": ""}); }
+ })",
+ QUrl());
+ QScopedPointer<QObject> root(component.create());
+ QVERIFY(root);
+ auto lm = qobject_cast<QQmlListModel *>(root.get());
+ QVERIFY(lm);
+ QJSValue val = lm->get(0);
+ QVERIFY(val.hasProperty("a"));
+ val = val.property("a");
+ QVERIFY(!val.isUndefined());
+ QVERIFY(val.isString());
+ QCOMPARE(val.toString(), QString());
+}
+
QTEST_MAIN(tst_qqmllistmodel)
#include "tst_qqmllistmodel.moc"