summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorChristoph Schleifenbaum <christoph.schleifenbaum@kdab.com>2014-08-25 12:29:18 +0200
committerChristoph Schleifenbaum <christoph.schleifenbaum@kdab.com>2014-08-29 07:26:29 +0200
commitfc4993be1fa7673016b6e5d81134463f163051f6 (patch)
treea6d100fc3478bd09557a67900844b4940ebd2579
parente4a778d9c261b6707cd9a3c4c174d8809a1cf9d3 (diff)
QListView: Catch stack overflow on mutual scrollbar calculation.
Task-number: QTBUG-39902 Change-Id: Ie850371098070e8ce485d5cb122aa89c18d97359 Reviewed-by: Marc Mutz <marc.mutz@kdab.com>
-rw-r--r--src/widgets/itemviews/qlistview.cpp34
-rw-r--r--tests/auto/widgets/itemviews/qlistview/tst_qlistview.cpp25
2 files changed, 57 insertions, 2 deletions
diff --git a/src/widgets/itemviews/qlistview.cpp b/src/widgets/itemviews/qlistview.cpp
index 135f89d4ac..5706be4b6d 100644
--- a/src/widgets/itemviews/qlistview.cpp
+++ b/src/widgets/itemviews/qlistview.cpp
@@ -1846,14 +1846,44 @@ void QCommonListViewBase::updateHorizontalScrollBar(const QSize &step)
{
horizontalScrollBar()->setSingleStep(step.width() + spacing());
horizontalScrollBar()->setPageStep(viewport()->width());
- horizontalScrollBar()->setRange(0, contentsSize.width() - viewport()->width());
+
+ // If both scroll bars are set to auto, we might end up in a situation with enough space
+ // for the actual content. But still one of the scroll bars will become enabled due to
+ // the other one using the space. The other one will become invisible in the same cycle.
+ // -> Infinite loop, QTBUG-39902
+ const bool bothScrollBarsAuto = qq->verticalScrollBarPolicy() == Qt::ScrollBarAsNeeded &&
+ qq->horizontalScrollBarPolicy() == Qt::ScrollBarAsNeeded;
+
+ if (bothScrollBarsAuto && contentsSize.width() - qq->verticalScrollBar()->width() <= viewport()->width()
+ && contentsSize.height() - qq->horizontalScrollBar()->height() <= viewport()->height()) {
+ // break the infinite loop described above by setting the range to 0, 0.
+ // QAbstractScrollArea will then hide the scroll bar for us
+ horizontalScrollBar()->setRange(0, 0);
+ } else {
+ horizontalScrollBar()->setRange(0, contentsSize.width() - viewport()->width());
+ }
}
void QCommonListViewBase::updateVerticalScrollBar(const QSize &step)
{
verticalScrollBar()->setSingleStep(step.height() + spacing());
verticalScrollBar()->setPageStep(viewport()->height());
- verticalScrollBar()->setRange(0, contentsSize.height() - viewport()->height());
+
+ // If both scroll bars are set to auto, we might end up in a situation with enough space
+ // for the actual content. But still one of the scroll bars will become enabled due to
+ // the other one using the space. The other one will become invisible in the same cycle.
+ // -> Infinite loop, QTBUG-39902
+ const bool bothScrollBarsAuto = qq->verticalScrollBarPolicy() == Qt::ScrollBarAsNeeded &&
+ qq->horizontalScrollBarPolicy() == Qt::ScrollBarAsNeeded;
+
+ if (bothScrollBarsAuto && contentsSize.width() - qq->verticalScrollBar()->width() <= viewport()->width()
+ && contentsSize.height() - qq->horizontalScrollBar()->height() <= viewport()->height()) {
+ // break the infinite loop described above by setting the range to 0, 0.
+ // QAbstractScrollArea will then hide the scroll bar for us
+ verticalScrollBar()->setRange(0, 0);
+ } else {
+ verticalScrollBar()->setRange(0, contentsSize.height() - viewport()->height());
+ }
}
void QCommonListViewBase::scrollContentsBy(int dx, int dy, bool /*scrollElasticBand*/)
diff --git a/tests/auto/widgets/itemviews/qlistview/tst_qlistview.cpp b/tests/auto/widgets/itemviews/qlistview/tst_qlistview.cpp
index b36b5aef8a..bb05db0b15 100644
--- a/tests/auto/widgets/itemviews/qlistview/tst_qlistview.cpp
+++ b/tests/auto/widgets/itemviews/qlistview/tst_qlistview.cpp
@@ -155,6 +155,7 @@ private slots:
void spacing();
void testScrollToWithHidden();
void testViewOptions();
+ void taskQTBUG_39902_mutualScrollBars();
};
// Testing get/set functions
@@ -2355,5 +2356,29 @@ void tst_QListView::testViewOptions()
QCOMPARE(options.decorationPosition, QStyleOptionViewItem::Top);
}
+void tst_QListView::taskQTBUG_39902_mutualScrollBars()
+{
+ QWidget window;
+ window.resize(400, 300);
+ QListView *view = new QListView(&window);
+ QStandardItemModel model(200, 1);
+ const QSize itemSize(100, 20);
+ for (int i = 0; i < model.rowCount(); ++i)
+ model.setData(model.index(i, 0), itemSize, Qt::SizeHintRole);
+ view->setModel(&model);
+
+ window.show();
+ QVERIFY(QTest::qWaitForWindowExposed(&window));
+ // make sure QListView is done with layouting the items (1/10 sec, like QListView)
+ QTest::qWait(100);
+
+ model.setRowCount(2);
+ for (int i = 0; i < model.rowCount(); ++i)
+ model.setData(model.index(i, 0), itemSize, Qt::SizeHintRole);
+ view->resize(itemSize.width() + view->frameWidth() * 2, model.rowCount() * itemSize.height() + view->frameWidth() * 2);
+ // this will end up in a stack overflow, if QTBUG-39902 is not fixed
+ QTest::qWait(100);
+}
+
QTEST_MAIN(tst_QListView)
#include "tst_qlistview.moc"