View Full Version : How can I show/hide a QScrollBar on top of a widget instead of next to it?

8th December 2011, 14:48
I have an application where I have a main widget that is controlled by a horizontal QScrollBar. The main widget is quite involved so I can't use a QScrollArea so I am left to using regular a QScrollBar instead. Ultimately I want the scrollbar to show only when I hover on top of the lower part of the main widget, and I want it to show on top of the widget rather than next to it.

The current implementation is something along the lines of:

class NestedMainWidget(QWidget):
def __init__(self, parent=None):
QWidget.__init__(self, parent)
self.main_widget = MainWidget(self)
self.scrollbar = QScrollBar(Qt.Horizontal, self)
self.box = QVBoxLayout()

The show/hide logic will then show/hide the scroll bar according to where the mouse position. This works fine. But obviously with this implementation the scroll bar will be shown next to the main widget, i.e., the main widget will be shrunk vertically to show the scroll bar.

How can I have the scroll bar to show on top of the main widget instead?

(The behavior I'm after is the scroll bar hiding that OS X Lion implements btw.)

9th December 2011, 04:42
Don't put the scroll bar in the layout if you don't want the layout to adjust when you make it visible. You will need to manually size and position the scroll bar when you make it visible.