0

I have a label that I pack into a scroll area. However, even though the label is bigger than the scrollable area, the scroll bars remain grayed out. Is there a way to make the scroll area recognize its content automatically so that it activates when needed?

Here some example code. It takes a screenshot when the button is pushed and the screenshot will be bigger than the scroll area:

from PIL.ImageQt import ImageQt
from PyQt5 import QtCore, QtGui, QtWidgets
from pyscreenshot import grab


class Ui_Form(object):
    def setupUi(self, Form):
        Form.setObjectName("Form")
        Form.resize(615, 481)
        self.pushButton = QtWidgets.QPushButton(Form)
        self.pushButton.setGeometry(QtCore.QRect(70, 30, 75, 23))
        self.pushButton.setObjectName("pushButton")
        self.scrollArea = QtWidgets.QScrollArea(Form)
        self.scrollArea.setGeometry(QtCore.QRect(70, 220, 120, 80))
        self.scrollArea.setWidgetResizable(True)
        self.scrollArea.setObjectName("scrollArea")
        self.scrollAreaWidgetContents = QtWidgets.QWidget()
        self.scrollAreaWidgetContents.setGeometry(QtCore.QRect(0, 0, 118, 78))
        self.scrollAreaWidgetContents.setObjectName("scrollAreaWidgetContents")
        self.label = QtWidgets.QLabel(self.scrollAreaWidgetContents)
        self.label.setGeometry(QtCore.QRect(40, 10, 47, 14))
        self.label.setObjectName("label")
        self.scrollArea.setWidget(self.scrollAreaWidgetContents)

        self.pushButton.clicked.connect(lambda: self.take_screenshot())

        self.retranslateUi(Form)
        self.pushButton.clicked.connect(self.label.show)
        QtCore.QMetaObject.connectSlotsByName(Form)

    def retranslateUi(self, Form):
        _translate = QtCore.QCoreApplication.translate
        Form.setWindowTitle(_translate("Form", "Form"))
        self.pushButton.setText(_translate("Form", "PushButton"))
        self.label.setText(_translate("Form", "TextLabel"))

    def take_screenshot(self):
        screenshot = grab()
        qim = ImageQt(screenshot).copy()
        pix = QtGui.QPixmap.fromImage(qim)
        self.label.setPixmap(pix)
        self.label.adjustSize()


if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    Form = QtWidgets.QWidget()
    ui = Ui_Form()
    ui.setupUi(Form)
    Form.show()
    sys.exit(app.exec_())
Nickpick
  • 6,163
  • 16
  • 65
  • 116
  • I suggest to use layouts for gui creation. You have put a label into a QWidget (scrollAreaWidgetContents), but without layouts, the size of this widget is fixed, even if the child widget is bigger than it – Fabio Apr 22 '20 at 09:10
  • I'm using qt designer. Can this be done from there? – Nickpick Apr 22 '20 at 09:12
  • Yes, right click on the scroll area (but outside the label) and select a layout. Also, you should *never* edit the files generated by pyuic. Recreate those files, and use a separate file for your main program, then imported those files as modules, as explained in the documentation about [using Designer](https://www.riverbankcomputing.com/static/Docs/PyQt5/designer.html). – musicamante Apr 22 '20 at 09:15
  • Great that works. Yes certainly doing this outside normally, but had to create an MWE for here. – Nickpick Apr 22 '20 at 17:26

0 Answers0