pythonlayoutpyqt5scrollbarqscrollarea

PyQt add a scrollbar to MainWindow


This a recuring question and i've read many topics some helped a bit (python Qt: main widget scroll bar, PyQt: Put scrollbars in this), some not at all (PyQt adding a scrollbar to my main window), I still have problem with the scrollbars. They're not usable, the're 'grey'.

Here is my code (I'm using PyQt5) :

def setupUi(self, Interface):
    Interface.setObjectName("Interface")
    Interface.resize(1152, 1009)
    sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Fixed, QtWidgets.QSizePolicy.Fixed)
    sizePolicy.setHorizontalStretch(0)
    sizePolicy.setVerticalStretch(0)
    sizePolicy.setHeightForWidth(Interface.sizePolicy().hasHeightForWidth())
    Interface.setSizePolicy(sizePolicy)
    Interface.setMouseTracking(False)
    icon = QtGui.QIcon()        

    self.centralWidget = QtWidgets.QWidget(Interface)
    self.centralWidget.setObjectName("centralWidget")

    self.scrollArea = QtWidgets.QScrollArea(self.centralWidget)
    self.scrollArea.setGeometry(QtCore.QRect(0, 0, 1131, 951))
    self.scrollArea.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOn)
    self.scrollArea.setHorizontalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOn)
    self.scrollArea.setWidgetResizable(True)
    self.scrollArea.setObjectName("scrollArea")
    self.scrollArea.setEnabled(True)

    self.scrollAreaWidgetContents = QtWidgets.QWidget()
    self.scrollAreaWidgetContents.setGeometry(QtCore.QRect(0, 0, 1112, 932))
    self.scrollAreaWidgetContents.setObjectName("scrollAreaWidgetContents")

    self.horizontalLayout = QtWidgets.QHBoxLayout(self.scrollAreaWidgetContents)
    self.horizontalLayout.setObjectName("horizontalLayout")

So i would like to put the scrollbars on the main widget, so if the user resizes the main window, the scrollbar appears, and let he move up and down to see child widgets that is outside the smaller window widget, allowing it to move right and left.

Help appreciated !


Solution

  • There are several things wrong with the example code. The main problems are that you are not using layouts properly, and the content widget is not being added to the scroll-area.

    Below is a fixed version (the commented lines are all junk, and can be removed):

    def setupUi(self, Interface):
        # Interface.setObjectName("Interface")
        # Interface.resize(1152, 1009)
        # sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Fixed, QtWidgets.QSizePolicy.Fixed)
        # sizePolicy.setHorizontalStretch(0)
        # sizePolicy.setVerticalStretch(0)
        # sizePolicy.setHeightForWidth(Interface.sizePolicy().hasHeightForWidth())
        # Interface.setSizePolicy(sizePolicy)
        # Interface.setMouseTracking(False)
        # icon = QtGui.QIcon()
        self.centralWidget = QtWidgets.QWidget(Interface)
        # self.centralWidget.setObjectName("centralWidget")
        layout = QtWidgets.QVBoxLayout(self.centralWidget)
    
        self.scrollArea = QtWidgets.QScrollArea(self.centralWidget)
        # self.scrollArea.setGeometry(QtCore.QRect(0, 0, 1131, 951))
        # self.scrollArea.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOn)
        # self.scrollArea.setHorizontalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOn)
        # self.scrollArea.setWidgetResizable(True)
        # self.scrollArea.setObjectName("scrollArea")
        # self.scrollArea.setEnabled(True)
        layout.addWidget(self.scrollArea)
    
        self.scrollAreaWidgetContents = QtWidgets.QWidget()
        self.scrollAreaWidgetContents.setGeometry(QtCore.QRect(0, 0, 1112, 932))
        # self.scrollAreaWidgetContents.setObjectName("scrollAreaWidgetContents")
    
        self.scrollArea.setWidget(self.scrollAreaWidgetContents)
    
        layout = QtWidgets.QHBoxLayout(self.scrollAreaWidgetContents)
        # self.horizontalLayout.setObjectName("horizontalLayout")
        # add child widgets to this layout...        
    
        Interface.setCentralWidget(self.centralWidget)