From 52c122e616f389bdeeffd3eedcd17c49e7e437c2 Mon Sep 17 00:00:00 2001 From: Christoph Schleifenbaum Date: Wed, 22 Apr 2015 13:06:32 +0200 Subject: Improve QListView scroll bar calculation. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit When both scroll bar's policies are set to ScrollBarAsNeeded, make sure the scroll bars are shown if needed and not show if not. Even the corner case, where one scroll bar's visibility depends on the other, is handled properly. Task-number: QTBUG-45470 Change-Id: I11d6ccf7c0b51644a5ce2d5c3fc59e2e4812755d Reviewed-by: Giuseppe D'Angelo Reviewed-by: Thorbjørn Lund Martsum Reviewed-by: Thorbjørn Lindeijer --- src/widgets/itemviews/qlistview.cpp | 26 ++++++++++++++++++++++---- 1 file changed, 22 insertions(+), 4 deletions(-) (limited to 'src/widgets') diff --git a/src/widgets/itemviews/qlistview.cpp b/src/widgets/itemviews/qlistview.cpp index b7a4ec3925..f3fd3e75a1 100644 --- a/src/widgets/itemviews/qlistview.cpp +++ b/src/widgets/itemviews/qlistview.cpp @@ -1846,8 +1846,17 @@ void QCommonListViewBase::updateHorizontalScrollBar(const QSize &step) 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()) { + const QSize viewportSize(viewport()->width() + (qq->verticalScrollBar()->maximum() > 0 ? qq->verticalScrollBar()->width() : 0), + viewport()->height() + (qq->horizontalScrollBar()->maximum() > 0 ? qq->horizontalScrollBar()->height() : 0)); + + bool verticalWantsToShow = contentsSize.height() > viewportSize.height(); + bool horizontalWantsToShow; + if (verticalWantsToShow) + horizontalWantsToShow = contentsSize.width() > viewportSize.width() - qq->verticalScrollBar()->width(); + else + horizontalWantsToShow = contentsSize.width() > viewportSize.width(); + + if (bothScrollBarsAuto && !horizontalWantsToShow) { // 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); @@ -1868,8 +1877,17 @@ void QCommonListViewBase::updateVerticalScrollBar(const QSize &step) 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()) { + const QSize viewportSize(viewport()->width() + (qq->verticalScrollBar()->maximum() > 0 ? qq->verticalScrollBar()->width() : 0), + viewport()->height() + (qq->horizontalScrollBar()->maximum() > 0 ? qq->horizontalScrollBar()->height() : 0)); + + bool horizontalWantsToShow = contentsSize.width() > viewportSize.width(); + bool verticalWantsToShow; + if (horizontalWantsToShow) + verticalWantsToShow = contentsSize.height() > viewportSize.height() - qq->horizontalScrollBar()->height(); + else + verticalWantsToShow = contentsSize.height() > viewportSize.height(); + + if (bothScrollBarsAuto && !verticalWantsToShow) { // 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); -- cgit v1.2.3