Search code examples
pythonpython-3.xqtpyqt

QPixmap scaled aspect ratio is not keep


aspectRatioMode=Qt.KeepAspectRatio does not work as expected, or I am getting something wrong

I am tring to open an image and resize it to fit my window, but I need to keep aspect ratio, what I've specified in aspectRatioMode=Qt.KeepAspectRatio, but in fact I get my image resized as it fills whole image_label.size()

    def open_image(self, image_path):
        pixmap = QPixmap(image_path)
        if not pixmap.isNull():
            scaled_pixmap = pixmap.scaled(self.image_label.size(), aspectRatioMode=Qt.KeepAspectRatio)
            self.image_label.setPixmap(scaled_pixmap)
        else:
            self.image_label.setText("Failed to load image")

Solution

  • I found the line that confused me and which brings to this result

    self.image_label.setScaledContents(True)

    Just needed to remove it and everything works fine