5

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 !

2 Answers 2

7

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)
Sign up to request clarification or add additional context in comments.

Comments

0

The scrollbars are grayed out because you made them always visible by setting the scrollbar policy to Qt.ScrollBarAlwaysOn but actually there is no content to be scrolled so they are disabled. If you want scrollbars to appear only when they are needed you need to use Qt.ScrollBarAsNeeded.

There is no content to be scrolled because there is only 1 widget in the QHBoxLayout (see self.scrollAreaWidgetContents). Also if this method is being executed from a QMainWindow you also have an error when setting the central widget: self.centralWidget is a method to retrieve the central widget. It's working because you are overwriting it with a QWidget instance (and I believe python allows you to do that). To correctly set the central widget you need to use setCentralWidget() in QMainWindow.

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.horizontalLayout = QtWidgets.QHBoxLayout()
    self.horizontalLayout.setObjectName("horizontalLayout")

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

    self.horizontalLayout.addWidget(self.scrollArea)

    centralWidget = QWidgets.QWidget()
    centralWidget.setObjectName("centralWidget")
    centralWidget.setLayout(self.horizontalLayout)

    self.setCentralWidget(centralWidget)

I left Interface out since I don't know what it is, but the rest should be ok.

3 Comments

Interface is actually the name of the QMain Window (sorry I should've specified that) and i have a code line Interface.setCentralWidget(self.centralWidget) further down, after all the Widgets have been declared. I still haven't been able to make it work. I don't quite understand what is self.scrollAreaWidgetContents doing. I thought it should be the QMainWindow, inside the QScrollBar and inside the QScrollBar all the Widgets/content.
The idea is the following. You need to setup a QScrollArea in which you will add the widgets you would like to scroll on. If you just want a QScrollArea as central widget you can directly set it using setCentralWidget. If you want multiple widgets instead, you need to lay them into a Layout, assign this layout to a QWidget, and set this one as central widget (like I did in the example above). Also if this setupUi method is in the QMainWindow subclass then you don't need to pass its instance as parameter since you access its members using self.
See also @ekhumoro answer which is more correct than mine.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.