How to hide scrollbar in QScrollArea?

橙三吉。 提交于 2020-05-05 17:10:10

问题


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


回答1:


Use this code:

QAbstractScrollArea::setHorizontalScrollBarPolicy( Qt::ScrollBarAlwaysOff )
QAbstractScrollArea::setVerticalScrollBarPolicy( Qt::ScrollBarAlwaysOff ) 



回答2:


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




回答3:


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




回答4:


This piece of code can do the job:

 setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
 verticalScrollBar()->hide();
 verticalScrollBar()->resize(0, 0);


来源:https://stackoverflow.com/questions/3383260/how-to-hide-scrollbar-in-qscrollarea

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!