aboutsummaryrefslogtreecommitdiffstats
diff options
-rw-r--r--src/qquicktreemodeladaptor.cpp6
-rw-r--r--tests/auto/tst_treeview.cpp16
2 files changed, 22 insertions, 0 deletions
diff --git a/src/qquicktreemodeladaptor.cpp b/src/qquicktreemodeladaptor.cpp
index 5a231af..08b8dbf 100644
--- a/src/qquicktreemodeladaptor.cpp
+++ b/src/qquicktreemodeladaptor.cpp
@@ -258,6 +258,9 @@ bool QQuickTreeModelAdaptor::childrenVisible(const QModelIndex &index)
QModelIndex QQuickTreeModelAdaptor::mapToModel(const QModelIndex &index) const
{
+ if (!index.isValid())
+ return QModelIndex();
+
const int row = index.row();
if (row < 0 || row > m_items.count() - 1)
return QModelIndex();
@@ -268,6 +271,9 @@ QModelIndex QQuickTreeModelAdaptor::mapToModel(const QModelIndex &index) const
QModelIndex QQuickTreeModelAdaptor::mapFromModel(const QModelIndex &index) const
{
+ if (!index.isValid())
+ return QModelIndex();
+
int row = -1;
for (int i = 0; i < m_items.count(); ++i) {
const QModelIndex proxyIndex = m_items[i].index;
diff --git a/tests/auto/tst_treeview.cpp b/tests/auto/tst_treeview.cpp
index 2fe9a36..61b9ff1 100644
--- a/tests/auto/tst_treeview.cpp
+++ b/tests/auto/tst_treeview.cpp
@@ -78,6 +78,7 @@ private slots:
void emptyModel();
void updatedModifiedModel();
void keypressOverload();
+ void invalidModelIndex();
};
QQuickView *tst_treeview::createView(const QString &filename)
@@ -265,6 +266,21 @@ void tst_treeview::keypressOverload()
QCOMPARE(parentPressed, Qt::Key_A);
}
+void tst_treeview::invalidModelIndex()
+{
+ // Check that you can call pass an invalid QModelIndex to the
+ // TreeView API without causing a crash / assert.
+ LOAD_TREEVIEW("normaltreeview.qml");
+ QMetaObject::invokeMethod(treeView, "isModelIndexExpanded", Q_ARG(QModelIndex, QModelIndex()));
+ QMetaObject::invokeMethod(treeView, "collapseModelIndex", Q_ARG(QModelIndex, QModelIndex()));
+ QMetaObject::invokeMethod(treeView, "expandModelIndex", Q_ARG(QModelIndex, QModelIndex()));
+ QMetaObject::invokeMethod(treeView, "toggleModelIndexExpanded", Q_ARG(QModelIndex, QModelIndex()));
+ QMetaObject::invokeMethod(treeView, "itemAtIndex", Q_ARG(QModelIndex, QModelIndex()));
+ QMetaObject::invokeMethod(treeView, "itemAtModelIndex", Q_ARG(QModelIndex, QModelIndex()));
+ QMetaObject::invokeMethod(treeView, "mapToModel", Q_ARG(QModelIndex, QModelIndex()));
+ QMetaObject::invokeMethod(treeView, "mapFromModel", Q_ARG(QModelIndex, QModelIndex()));
+}
+
QTEST_MAIN(tst_treeview)
#include "tst_treeview.moc"