QPixmap 缩放时未保持宽高比
aspectRatioMode=Qt.KeepAspectRatio 这个设置没有按预期工作,或者我可能搞错了什么。
我想打开一张图片,并把它调整大小以适应我的窗口,但我需要保持图片的宽高比。我在 aspectRatioMode=Qt.KeepAspectRatio 中指定了这一点,但实际上我得到的图片是被调整到填满整个 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")
1 个回答
0
我找到了让我困惑的那一行代码,它导致了这个结果。
self.image_label.setScaledContents(True)
只需要把它去掉,其他的就都正常了。