From 4bd27d5c8c1f2dd759e20e4a6b3ac224799e2318 Mon Sep 17 00:00:00 2001 From: Bea Lam Date: Thu, 23 Sep 2010 13:05:48 +1000 Subject: Fix ListModel::set() so the implementation and docs are consistent. If index == count() the item should be appended. Also, this should happen regardless of whether the list is empty. --- src/declarative/util/qdeclarativelistmodel.cpp | 5 +++-- .../declarative/qdeclarativelistmodel/tst_qdeclarativelistmodel.cpp | 2 +- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/src/declarative/util/qdeclarativelistmodel.cpp b/src/declarative/util/qdeclarativelistmodel.cpp index 93a38f4..398480e 100644 --- a/src/declarative/util/qdeclarativelistmodel.cpp +++ b/src/declarative/util/qdeclarativelistmodel.cpp @@ -552,7 +552,8 @@ QScriptValue QDeclarativeListModel::get(int index) const fruitModel.set(3, {"cost": 5.95, "name":"Pizza"}) \endcode - The \a index must be an element in the list. + If \a index is equal to count() then a new item is appended to the + list. Otherwise, \a index must be an element in the list. \sa append() */ @@ -562,7 +563,7 @@ void QDeclarativeListModel::set(int index, const QScriptValue& valuemap) qmlInfo(this) << tr("set: value is not an object"); return; } - if (count() == 0 || index > count() || index < 0) { + if (index > count() || index < 0) { qmlInfo(this) << tr("set: index %1 out of range").arg(index); return; } diff --git a/tests/auto/declarative/qdeclarativelistmodel/tst_qdeclarativelistmodel.cpp b/tests/auto/declarative/qdeclarativelistmodel/tst_qdeclarativelistmodel.cpp index 31cb545..69df90b 100644 --- a/tests/auto/declarative/qdeclarativelistmodel/tst_qdeclarativelistmodel.cpp +++ b/tests/auto/declarative/qdeclarativelistmodel/tst_qdeclarativelistmodel.cpp @@ -260,7 +260,7 @@ void tst_qdeclarativelistmodel::dynamic_data() QTest::newRow("set2") << "{append({'foo':123});set(0,{'foo':456});get(0).foo}" << 456 << ""; QTest::newRow("set3a") << "{append({'foo':123,'bar':456});set(0,{'foo':999});get(0).foo}" << 999 << ""; QTest::newRow("set3b") << "{append({'foo':123,'bar':456});set(0,{'foo':999});get(0).bar}" << 456 << ""; - QTest::newRow("set4a") << "{set(0,{'foo':456})}" << 0 << ": QML ListModel: set: index 0 out of range"; + QTest::newRow("set4a") << "{set(0,{'foo':456});count}" << 1 << ""; QTest::newRow("set4c") << "{set(-1,{'foo':456})}" << 0 << ": QML ListModel: set: index -1 out of range"; QTest::newRow("set5a") << "{append({'foo':123,'bar':456});set(0,123);count}" << 1 << ": QML ListModel: set: value is not an object"; QTest::newRow("set5b") << "{append({'foo':123,'bar':456});set(0,[1,2,3]);count}" << 1 << ": QML ListModel: set: value is not an object"; -- cgit v0.12