Search code examples
pythonpython-2.7pyside2

I would like to add a scroll bar to a QVBoxLayout


How do I add a scroll bar to my QVBoxLayout in PySide2.

self.mainWidget = QtWidgets.QWidget()
self.window.setCentralWidget(self.mainWidget)
self.vertical_layout_main = QtWidgets.QVBoxLayout(self.mainWidget)       
scroll = QtWidgets.QScrollArea()
scroll.setWidget(self.vertical_layout_main)
scroll.setFixedHeight(400)
self.vertical_layout_main.addWidget(scroll)

Update:

def populate_lights(self):
    self.vertical_layout_lights = QtWidgets.QVBoxLayout(self.mainWidget)
    self.vertical_layout_main.addLayout(self.vertical_layout_lights)
    for light in self.lights:
        horizontal_layout_light = QtWidgets.QVBoxLayout(self.mainWidget)
        light_label = QtWidgets.QPushButton(light)
        light_label.setCheckable(True)
        light_label.toggled.connect(partial(self.light_label_event,light))
        horizontal_layout_light.addWidget(light_label)
        self.vLayout.addLayout(horizontal_layout_light)

def light_palette_ui(self): 

    self.vertical_layout_main = QtWidgets.QVBoxLayout(self.mainWidget)
    self.scroll_widget = QtWidgets.QWidget()
    self.scroll_widget.setLayout(self.vertical_layout_main)
    self.scroll = QtWidgets.QScrollArea()
    self.scroll.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOn)
    self.scroll.setHorizontalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff)
    self.scroll.setWidgetResizable(False)
    self.scroll.setWidget(self.scroll_widget)
    self.vLayout = QtWidgets.QVBoxLayout(self.mainWidget)
    self.vLayout.addWidget(self.scroll)
    self.populate_lights()        
    self.window.setAttribute(QtCore.Qt.WA_DeleteOnClose)
    self.window.show()        

Solution

  • setWidget() is used to add the widget to the QScrollArea, in the following part I show an example:

    from PySide2 import QtWidgets
    
    
    class Test:
        def __init__(self):
            self.window = QtWidgets.QMainWindow()
            self.mainWidget = QtWidgets.QWidget()
            self.window.setCentralWidget(self.mainWidget)
            self.vertical_layout_main = QtWidgets.QVBoxLayout(self.mainWidget)       
            scroll = QtWidgets.QScrollArea()
    
            content_widget = QtWidgets.QWidget()
            scroll.setWidget(content_widget)
            scroll.setWidgetResizable(True)
    
            lay = QtWidgets.QVBoxLayout(content_widget)
    
            for l in "ABCDEFGHIJKLMNOPQRSTUVWXYZ":
                btn = QtWidgets.QPushButton(l)
                lay.addWidget(btn)
            lay.addStretch()
    
            scroll.setFixedHeight(400)
    
            self.vertical_layout_main.addWidget(scroll)
            self.window.show()
    
    
    if __name__ == '__main__':
        import sys
        app = QtWidgets.QApplication(sys.argv)
        t = Test()
        sys.exit(app.exec_())
    

    Update:

    from PySide2 import QtCore, QtWidgets
    from functools import partial
    
    class Test:
        def __init__(self):
            self.window = QtWidgets.QMainWindow()
            self.mainWidget = QtWidgets.QWidget()
            self.lights = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J"]
            self.window.setCentralWidget(self.mainWidget)
    
        def light_label_event(self, name, checked):
            print(name,checked)
    
        def populate_lights(self):
            self.light_layout = QtWidgets.QVBoxLayout(self.scroll_widget)
            for light in self.lights:
                light_label = QtWidgets.QPushButton(light)
                light_label.setCheckable(True)
                light_label.toggled.connect(partial(self.light_label_event,light))
                self.light_layout.addWidget(light_label)
            self.light_layout.addStretch()
    
        def light_palette_ui(self):
            self.vertical_layout_main = QtWidgets.QVBoxLayout(self.mainWidget)
            self.scroll = QtWidgets.QScrollArea()
            self.scroll.setWidgetResizable(True)
            self.vertical_layout_main.addWidget(self.scroll)
    
            self.scroll.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOn)
            self.scroll.setHorizontalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff)
    
            self.scroll_widget = QtWidgets.QWidget()
            self.scroll.setWidget(self.scroll_widget)
    
            self.populate_lights()        
            self.window.setAttribute(QtCore.Qt.WA_DeleteOnClose)
            self.window.show() 
    
    
    if __name__ == '__main__':
        import sys
        app = QtWidgets.QApplication(sys.argv)
        t = Test()
        t.light_palette_ui()
        sys.exit(app.exec_())