It is a common problem to display a QImage in a widget. While this can be done using QLabel.setPixmap
, the resulting QLabel will have a fixed size equal to the size of the pixmap. It is possible to use setScaledContents
to make QLabel scale the pixmap and allow for resizing. However, this will ignore the aspect ratio of the image and scale the pixmap to fill the whole label.
Several other questions on StackOverflow ask for solutions to that problem, and the typical solution given is to re-scale the pixmap using QPixmap.scaled()
depending on the size of the widget:
- QPixmap maintain aspect ratio python
- How do I make an image resize to scale in Qt?
- Qt: resizing a QLabel, containing a QPixmap, while keeping it's aspect ratio
Is there a different, more "native" way to achieve this?