How to hide scrollbar in QScrollArea?

2020-05-01 09:36发布

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().

4条回答
萌系小妹纸
2楼-- · 2020-05-01 09:41

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).

查看更多
别忘想泡老子
3楼-- · 2020-05-01 09:52

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().

查看更多
神经病院院长
4楼-- · 2020-05-01 09:55

Use this code:

QAbstractScrollArea::setHorizontalScrollBarPolicy( Qt::ScrollBarAlwaysOff )
QAbstractScrollArea::setVerticalScrollBarPolicy( Qt::ScrollBarAlwaysOff ) 
查看更多
Lonely孤独者°
5楼-- · 2020-05-01 10:00

This piece of code can do the job:

 setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
 verticalScrollBar()->hide();
 verticalScrollBar()->resize(0, 0);
查看更多
登录 后发表回答