The main window of my PyQt5 application is set up with a text label along the top above a custom canvas widget which displays an image:
self.canvas = Canvas() # a custom QtWidgets.QWidget subclass self.label = QtWidgets.QLabel() self.label.setText('foobar') self.label.setSizePolicy(QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Fixed) layout = QtWidgets.QVBoxLayout() layout.addWidget(self.label) layout.addWidget(self.canvas) layout.setSpacing(0) layout.setContentsMargins(0, 0, 0, 0) content = QtWidgets.QWidget() content.setLayout(layout) self.setCentralWidget(content)
This looks like this, which is what I want:
When the canvas changes to display a smaller image, I want to shrink the window to fit accordingly. I tried this when the image changes:
self.canvas.setFixedSize(image.width(), image.height()) self.adjustSize()
It seems that the minimum size that I can give the window if I manually drag to resize it is the size that fits the contents, but why isn’t it resizing to this automatically?