pythonscrollbarpyside6pyqt6qdockwidget

How to add a scrollbar in qdockwidget in pyqt?


I want to add a scrollbar inside the dockwidget and let it fit the wnd size, and the dockwidget seems re-dock not work.

import sys
from PySide6.QtWidgets import *
from PySide6.QtGui import *
from PySide6.QtCore import *

class My_wgt(QWidget):
    def __init__(self, parent=None):
        super().__init__(parent)
        layout = QVBoxLayout()
        lineedit_list = [QLabel(f"Line {i}") for i in range(30)]

        for i in range(30):
            layout.addWidget(lineedit_list[i])

        self.setLayout(layout)


class My_wnd(QMainWindow):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.setFixedHeight(500)

        self.dock = QDockWidget("Dock")
        self.dock.setFloating(False)

        self.dock_in_wgt = My_wgt(self.dock)
        self.dock_in_wgt.setFixedWidth(250)

        self.dock.setWidget(self.dock_in_wgt)
        self.dock.setAllowedAreas(Qt.RightDockWidgetArea)
        self.addDockWidget(Qt.RightDockWidgetArea, self.dock)

if __name__ == "__main__":
    app = QApplication(sys.argv)
    main_window = My_wnd()
    main_window.show()
    sys.exit(app.exec())

Heres the code using pyside6, hope somebody can help me. what I want to add


Solution

  • If you want to add a scrollbar to your DockWidget and make it fit the window size, you should place your QVBoxLayout into a QScrollArea instead of directly placing it onto a widget.

    import sys
    from PySide6.QtWidgets import *
    from PySide6.QtCore import *
    
    class My_wgt(QWidget):
        def __init__(self, parent=None):
            super().__init__(parent)
            layout = QVBoxLayout(self)
            scroll_area = QScrollArea(self)
            container = QWidget()
            container_layout = QVBoxLayout()
    
            lineedit_list = [QLabel(f"Line {i}") for i in range(30)]
            for label in lineedit_list:
                container_layout.addWidget(label)
    
            container.setLayout(container_layout)
            scroll_area.setWidget(container)
            scroll_area.setWidgetResizable(True)
            layout.addWidget(scroll_area)
    
    class My_wnd(QMainWindow):
        def __init__(self, parent=None):
            super().__init__(parent)
            self.setFixedHeight(500)
    
            self.dock = QDockWidget("Dock", self)
            self.dock.setFloating(False)
    
            self.dock_in_wgt = My_wgt(self.dock)
    
            sizePolicy = QSizePolicy(QSizePolicy.Preferred, QSizePolicy.Preferred)
            sizePolicy.setHorizontalStretch(1)
            self.dock_in_wgt.setSizePolicy(sizePolicy)
    
            self.dock.setWidget(self.dock_in_wgt)
            self.dock.setAllowedAreas(Qt.RightDockWidgetArea)
            self.addDockWidget(Qt.RightDockWidgetArea, self.dock)
    
    if __name__ == "__main__":
        app = QApplication(sys.argv)
        main_window = My_wnd()
        main_window.show()
        sys.exit(app.exec())