11

How can one hide the scrollbars in a QScrollArea? Currently I use the hide() method on the scrollbars returned by QScrollArea::horizontalScrollBar() and QScrollArea::verticalScrollBar() but the space reserved for scrollbars still remains. Obviously this looks very ugly and is not space efficient. If I remove the scrollbars altogether I can no longer easily scroll to a specific point using QScrollBar::setValue().

ymoreau
  • 3,402
  • 1
  • 22
  • 60
pafcu
  • 7,808
  • 12
  • 42
  • 55

4 Answers4

21

You can hide it using a style sheet. Use height:0px; to hide the horizontal scroll bar and width=0px; to hide the vertical scroll bar. Like that:

horizontalScrollBar()->setStyleSheet("QScrollBar {height:0px;}");
verticalScrollBar()->setStyleSheet("QScrollBar {width:0px;}");

And voila!.No scroll bars, and you can still manipulate them using setValue().

MadeOfAir
  • 2,933
  • 5
  • 31
  • 39
19

Use this code:

QAbstractScrollArea::setHorizontalScrollBarPolicy( Qt::ScrollBarAlwaysOff )
QAbstractScrollArea::setVerticalScrollBarPolicy( Qt::ScrollBarAlwaysOff ) 
Roman Byshko
  • 8,591
  • 7
  • 35
  • 57
Mason Zhang
  • 3,423
  • 24
  • 35
  • 2
    If I do it that way I can no longer scroll to a specific point using QScrollBar::setValue() (because the scrollbars do not exist) – pafcu Aug 02 '10 at 11:32
  • 3
    If you need a scroll bar when needed, use the policy: Qt::ScrollBarAsNeeded. If you want to scroll content programmatic, use QAbstractScrollArea::scrollContentsBy ( int dx, int dy ) – Mason Zhang Aug 03 '10 at 02:03
  • 1
    [Apparenly](http://doc.qt.io/qt-5/qabstractscrollarea.html#scrollContentsBy) you should call scrollbar's `setValue()` instead of `scrollContentsBy()`. – Oliver Jan 07 '17 at 18:55
3

This piece of code can do the job:

 setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
 verticalScrollBar()->hide();
 verticalScrollBar()->resize(0, 0);
Roman Byshko
  • 8,591
  • 7
  • 35
  • 57
1

From Qt documents for scrollContentsBy():

Calling this function in order to scroll programmatically is an error, use the scroll bars instead (e.g. by calling QScrollBar::setValue() directly).

Martin Buberl
  • 45,844
  • 25
  • 100
  • 144
bootchk
  • 1,926
  • 17
  • 14