summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorBea Lam <bea.lam@nokia.com>2010-05-13 05:21:38 (GMT)
committerBea Lam <bea.lam@nokia.com>2010-05-13 05:21:38 (GMT)
commit5220b879a19b4fa1e28829e724c1ad12e85566f4 (patch)
tree43143988e6c0affd3223c879d4d118a5e0453fd9
parentffdb6fcca3bf8f4cc97f8bfe09a43811c29a7dbe (diff)
downloadQt-5220b879a19b4fa1e28829e724c1ad12e85566f4.zip
Qt-5220b879a19b4fa1e28829e724c1ad12e85566f4.tar.gz
Qt-5220b879a19b4fa1e28829e724c1ad12e85566f4.tar.bz2
ListModel::get() should return undefined if bad index specified
-rw-r--r--src/declarative/util/qdeclarativelistmodel.cpp28
-rw-r--r--tests/auto/declarative/qdeclarativelistmodel/tst_qdeclarativelistmodel.cpp4
2 files changed, 21 insertions, 11 deletions
diff --git a/src/declarative/util/qdeclarativelistmodel.cpp b/src/declarative/util/qdeclarativelistmodel.cpp
index a8a445a..a8e1be8 100644
--- a/src/declarative/util/qdeclarativelistmodel.cpp
+++ b/src/declarative/util/qdeclarativelistmodel.cpp
@@ -537,10 +537,9 @@ void QDeclarativeListModel::append(const QScriptValue& valuemap)
*/
QScriptValue QDeclarativeListModel::get(int index) const
{
- if (index >= count() || index < 0) {
+ // the internal flat/nested class takes care of return value for bad index
+ if (index >= count() || index < 0)
qmlInfo(this) << tr("get: index %1 out of range").arg(index);
- return 0;
- }
return m_flat ? m_flat->get(index) : m_nested->get(index);
}
@@ -930,13 +929,14 @@ bool FlatListModel::insert(int index, const QScriptValue &value)
QScriptValue FlatListModel::get(int index) const
{
- Q_ASSERT(index >= 0 && index < m_values.count());
-
QScriptEngine *scriptEngine = m_scriptEngine ? m_scriptEngine : QDeclarativeEnginePrivate::getScriptEngine(qmlEngine(m_listModel));
- if (!scriptEngine)
+ if (!scriptEngine)
return 0;
+ if (index < 0 || index >= m_values.count())
+ return scriptEngine->undefinedValue();
+
QScriptValue rv = scriptEngine->newObject();
QHash<int, QVariant> row = m_values.at(index);
@@ -1183,13 +1183,21 @@ bool NestedListModel::append(const QScriptValue& valuemap)
}
QScriptValue NestedListModel::get(int index) const
-{
- ModelNode *node = qvariant_cast<ModelNode *>(_root->values.at(index));
- if (!node)
- return 0;
+{
QDeclarativeEngine *eng = qmlEngine(m_listModel);
if (!eng)
return 0;
+
+ if (index < 0 || index >= count()) {
+ QScriptEngine *seng = QDeclarativeEnginePrivate::getScriptEngine(eng);
+ if (seng)
+ return seng->undefinedValue();
+ return 0;
+ }
+
+ ModelNode *node = qvariant_cast<ModelNode *>(_root->values.at(index));
+ if (!node)
+ return 0;
return QDeclarativeEnginePrivate::qmlScriptObject(node->object(this), eng);
}
diff --git a/tests/auto/declarative/qdeclarativelistmodel/tst_qdeclarativelistmodel.cpp b/tests/auto/declarative/qdeclarativelistmodel/tst_qdeclarativelistmodel.cpp
index ec97461..aed4781 100644
--- a/tests/auto/declarative/qdeclarativelistmodel/tst_qdeclarativelistmodel.cpp
+++ b/tests/auto/declarative/qdeclarativelistmodel/tst_qdeclarativelistmodel.cpp
@@ -282,7 +282,9 @@ void tst_qdeclarativelistmodel::dynamic()
int actual = e.evaluate().toInt();
if (e.hasError())
qDebug() << e.error(); // errors not expected
- QVERIFY(!e.hasError());
+
+ if (QTest::currentDataTag() != QLatin1String("clear3") && QTest::currentDataTag() != QLatin1String("remove3"))
+ QVERIFY(!e.hasError());
QCOMPARE(actual,result);
}