Issue
I have been trying to display an image in a QGridLayout, and resize it according to the space available to it. However I can't seem to get the right size.
I have created a class ImageWidget(Qwidget)
, which is what is referenced as self
below, and an instance of which ImageWidget0
is added to the QGridLayout Layout
with Layout.addWidget(ImageWidget0, 0, 0)
.
This is how I'm resizing the pixmap, and it is displaying the image in the layout at the specified size with no problems:
self.label = QLabel(self)
self.pixmap = self.pixmap.scaled(image_size[0], image_size[1], Qt.KeepAspectRatio)
self.label.setPixmap(self.pixmap)
self.show()
However different methods of assigning image_size
are giving undesirable results. Both of the following, using the size of the widget and the size of the label respectively, produce an image_size
of (100, 30), which is too small:
image_size = (self.width(), self.height())
image_size = (self.label.width(), self.label.height())
So I tried to get the size of the rows and columns instead using the only methods I could find in the QGridLayout documentation, but the following (perhaps unsuprisingly) both produce an image_size
of (0, 0):
image_size = (self.parent().layout.columnMinimumWidth(0), self.parent().layout.rowMinimumHeight(0))
image_size = (self.parentWidget().layout.columnMinimumWidth(0), self.parentWidget().layout.rowMinimumHeight(0))
To be clear: The layout itself is NOT CHANGING for all these different pixmap sizes. If image_size
is manually set too large, it clips out (which is fine), but when it is too small, it just shrinks in the available space. This is what make me think the problem doesn't lie in the layout adjusting to the pixmap size.
I can post more code if required, but I was trying to keep this succinct and to the point.
Solution
As @musicamante pointed out, it is pointless using the widget size at construction as the layout has not yet been set. I got around this by adding a "load image" button, which could add the image once the layout had been created, allowing me to set the size correctly using:
image_size = (self.width(), self.height())
image_size = (self.width(), self.height())
Answered By - S. Dunnim
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.