summaryrefslogtreecommitdiffstats
path: root/tests/auto/widgets/itemviews
diff options
context:
space:
mode:
authorVolker Hilsheimer <volker.hilsheimer@qt.io>2023-12-13 12:47:36 +0100
committerVolker Hilsheimer <volker.hilsheimer@qt.io>2023-12-14 22:59:33 +0100
commit04415264489cd96a4a542a2ae7db1c14558397a5 (patch)
treed7bb102efefdfd8523e4530008258ead2fa5058e /tests/auto/widgets/itemviews
parent1b71e2d894c2be7052518cdcb96020c9950e2dc7 (diff)
QAIV: reset state if removing index while editing or committing
If the commit of data to a model index result in that index getting filtered out, then the editor will be removed as part of committing the data, and the index that the editor is associated with becomes invalid. The subsequent call to closeEditor() will find that the index for the editor is no longer valid. This should not warn, and it should also not abort the clean-up process early. Instead, identify that the editor that we want to closed is already hidden. In that case, skip the warning and most of the cleanup process, and proceed directly to the EndEditHint handling further down. Add a test that simulates the two scenarios where this can happen: either the committing of data results in the index being filtered out by the existing filter; or the filter changes while editing, and the index being edited gets removed. In both cases, we don't want to see a warning, and the state of the item view should be reset correctly. Pick-to: 6.7 6.6 6.5 Fixes: QTBUG-115765 Change-Id: If247172fdac9a1a9279dae96c078d32553d4ee5d Reviewed-by: Axel Spoerl <axel.spoerl@qt.io> Reviewed-by: 🌴 Alexey Edelev 🌴 <alexey.edelev@qt.io>
Diffstat (limited to 'tests/auto/widgets/itemviews')
-rw-r--r--tests/auto/widgets/itemviews/qabstractitemview/tst_qabstractitemview.cpp58
1 files changed, 58 insertions, 0 deletions
diff --git a/tests/auto/widgets/itemviews/qabstractitemview/tst_qabstractitemview.cpp b/tests/auto/widgets/itemviews/qabstractitemview/tst_qabstractitemview.cpp
index 22d4d5b66d..15a7d2319e 100644
--- a/tests/auto/widgets/itemviews/qabstractitemview/tst_qabstractitemview.cpp
+++ b/tests/auto/widgets/itemviews/qabstractitemview/tst_qabstractitemview.cpp
@@ -149,6 +149,7 @@ private slots:
void selectionAutoScrolling();
void testSpinBoxAsEditor_data();
void testSpinBoxAsEditor();
+ void removeIndexWhileEditing();
private:
static QAbstractItemView *viewFromString(const QByteArray &viewType, QWidget *parent = nullptr)
@@ -3463,5 +3464,62 @@ void tst_QAbstractItemView::testSpinBoxAsEditor()
QCOMPARE(model.data(model.index(0, 1)).toInt(), 1);
}
+void tst_QAbstractItemView::removeIndexWhileEditing()
+{
+ QTreeView view;
+ QStandardItemModel treeModel;
+ auto editableItem1 = new QStandardItem("aa");
+ auto editableItem2 = new QStandardItem("ab");
+ auto editableItem3 = new QStandardItem("ac");
+ auto item = new QStandardItem("a");
+ item->appendRow(editableItem1);
+ item->appendRow(editableItem2);
+ item->appendRow(editableItem3);
+ treeModel.setItem(0, 0, item);
+ QSortFilterProxyModel filterModel;
+ filterModel.setSourceModel(&treeModel);
+ view.setModel(&filterModel);
+ view.show();
+
+ QVERIFY(QTest::qWaitForWindowExposed(&view));
+
+ view.setExpanded(item->index(), true);
+
+ filterModel.setFilterRegularExpression("a.*");
+
+ QTest::failOnWarning(QRegularExpression("QAbstractItemView::closeEditor called with an editor "
+ "that does not belong to this view"));
+
+ // Verify that we shut editing down cleanly if the index we are editing is
+ // filtered out after committing
+ {
+ const QModelIndex filteredIndex = filterModel.mapFromSource(editableItem1->index());
+ QVERIFY(filteredIndex.isValid());
+ view.edit(filteredIndex);
+ QCOMPARE(view.state(), QAbstractItemView::EditingState);
+ QPointer<QLineEdit> lineEdit = qobject_cast<QLineEdit *>(QApplication::focusWidget());
+ QVERIFY(lineEdit);
+ lineEdit->setText("c");
+ QTest::keyClick(lineEdit, Qt::Key_Enter);
+ QTRY_VERIFY(!lineEdit);
+ QCOMPARE(editableItem1->data(Qt::DisplayRole), "c");
+ QCOMPARE(view.state(), QAbstractItemView::NoState);
+ }
+
+ // If we change the filter while we edit, then we should clean up state as well
+ {
+ const QModelIndex filteredIndex = filterModel.mapFromSource(editableItem2->index());
+ QVERIFY(filteredIndex.isValid());
+ view.edit(filteredIndex);
+ QCOMPARE(view.state(), QAbstractItemView::EditingState);
+ QPointer<QLineEdit> lineEdit = qobject_cast<QLineEdit *>(QApplication::focusWidget());
+ QVERIFY(lineEdit);
+ filterModel.setFilterFixedString("c");
+ QVERIFY(!filterModel.mapFromSource(editableItem2->index()).isValid());
+ QTRY_VERIFY(!lineEdit);
+ QCOMPARE(view.state(), QAbstractItemView::NoState);
+ }
+}
+
QTEST_MAIN(tst_QAbstractItemView)
#include "tst_qabstractitemview.moc"