aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRichard Moe Gustavsen <richard.gustavsen@qt.io>2021-05-19 10:56:22 +0200
committerRichard Moe Gustavsen <richard.gustavsen@qt.io>2021-05-19 11:46:24 +0200
commitfa0a6426f7a63db61358ebcf4d67538d049a1491 (patch)
tree8cf0c1c458c1e683cd4f4c3b9a78dc173cc3278f
parentce38ab33fbba4ea8a5f1eda85713f217b8353486 (diff)
Check that modelIndex is valid before trying to use it6.1
At start-up, m_currentIndex is not yet set. So if the app tries to access e.g currentItem, it will cause an assert to happen in QQuickTableView. This patch will do some extra checking to avoid this. Fixes: QTBUG-93843 Change-Id: I461fa5a90d822a65795acabe36c18afa38f4810d Reviewed-by: Mitch Curtis <mitch.curtis@qt.io>
-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"