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_())