QListView: Catch stack overflow on mutual scrollbar calculation.

Task-number: QTBUG-39902
Change-Id: Ie850371098070e8ce485d5cb122aa89c18d97359
Reviewed-by: Marc Mutz <marc.mutz@kdab.com>
This commit is contained in:
Christoph Schleifenbaum 2014-08-25 12:29:18 +02:00
parent e4a778d9c2
commit fc4993be1f
2 changed files with 57 additions and 2 deletions

View File

@ -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*/)

View File

@ -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"