summaryrefslogtreecommitdiffstats
path: root/tests/auto/widgets/itemviews
diff options
context:
space:
mode:
authorVolker Hilsheimer <volker.hilsheimer@qt.io>2024-04-15 17:00:19 +0200
committerVolker Hilsheimer <volker.hilsheimer@qt.io>2024-04-17 04:38:33 +0200
commit960867566a75931c338f67cfe429c0756f41ad89 (patch)
treeff14f3e705aa7df307fc1003f9d35b62db48bc93 /tests/auto/widgets/itemviews
parent62d02c1d509780eca7d3a4c35ac64a0d475d0fe7 (diff)
QTableView: fix infinite loop when resizing rows/columns on model reset
When a slot is connected to the modelReset signal before the model is set on the view, then such a slot might try to resize the columns or rows to fit the contents before the view had a chance to perform layout work. In that case, the while-loop that calculated the size hint of a rows (or column) by checking the size hint of each cell might not exit, as cached information about visual indexes would be invalid. To prevent this, don't loop over the cells if the actual last row/column of the model (after the reset) is less than the first (cached) visible row/column. Also, make sure we increase the counter for number of rows/columns processed with each iteration. Add a test (that loops infinitely without the fix, resulting in the test being killed by the framework), and make the 'actual' variables constant for clarity. Fixes: QTBUG-124301 Pick-to: 6.7 Change-Id: I0adb2f1e8a1e78760ef7c19e9686c9572eca8be6 Reviewed-by: Axel Spoerl <axel.spoerl@qt.io>
Diffstat (limited to 'tests/auto/widgets/itemviews')
-rw-r--r--tests/auto/widgets/itemviews/qtableview/tst_qtableview.cpp30
1 files changed, 30 insertions, 0 deletions
diff --git a/tests/auto/widgets/itemviews/qtableview/tst_qtableview.cpp b/tests/auto/widgets/itemviews/qtableview/tst_qtableview.cpp
index 55de7d6b23..45a467d423 100644
--- a/tests/auto/widgets/itemviews/qtableview/tst_qtableview.cpp
+++ b/tests/auto/widgets/itemviews/qtableview/tst_qtableview.cpp
@@ -391,6 +391,7 @@ private slots:
void resizeToContents();
void resizeToContentsSpans();
+ void resizeToContentsEarly();
void tabFocus();
void bigModel();
@@ -3777,6 +3778,35 @@ void tst_QTableView::resizeToContentsSpans()
QCOMPARE(view2.columnWidth(0), view3.columnWidth(0) - view2.columnWidth(1));
}
+void tst_QTableView::resizeToContentsEarly()
+{
+ QStringListModel model;
+ QTableView view;
+
+ // connect to the model before setting it on the view
+ connect(&model, &QStringListModel::modelReset, &model, [&view]{
+ view.resizeColumnsToContents();
+ });
+ connect(&model, &QStringListModel::modelReset, &model, [&view]{
+ view.resizeRowsToContents();
+ });
+
+ // the view only connects now to the model's signals, so responds to the
+ // reset signal *after* the lambdas above
+ view.setModel(&model);
+
+ QStringList data(200, QString("Hello World"));
+ model.setStringList(data);
+
+ view.show();
+ QVERIFY(QTest::qWaitForWindowExposed(&view));
+
+ view.verticalScrollBar()->setValue(view.verticalScrollBar()->maximum());
+
+ data = data.sliced(data.size() / 2);
+ model.setStringList(data);
+}
+
QT_BEGIN_NAMESPACE
extern bool Q_WIDGETS_EXPORT qt_tab_all_widgets(); // qapplication.cpp
QT_END_NAMESPACE