Try to search your question here, if you can't find : Ask Any Question Now ?

Resizing a QWindow to fit contents

HomeCategory: stackoverflowResizing a QWindow to fit contents
Avatarjohn asked 1 week ago

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:

enter image description here

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

enter image description here

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?

1 Answers
Best Answer
AvatarAmit answered 1 week ago
Your Answer

1 + 14 =

Popular Tags

WP Facebook Auto Publish Powered By : XYZScripts.com