摆脱
self.myLabel.setScaledContents(True)
调用(或将其设置为 False)。它用像素图填充您的小部件,而不关心宽高比。
如果您需要调整大小QPixmap
,正如你所发现的,scaled
是所需的方法。但你调用它是错误的。我们看一下定义:
QPixmap QPixmap.scaled (self,
int width,
int height,
Qt.AspectRatioMode aspectRatioMode = Qt.IgnoreAspectRatio,
Qt.TransformationMode transformMode = Qt.FastTransformation)
该函数的返回类型是QPixmap
,所以它返回一个缩放的copy原始像素图。
那么你需要一个width
and a height
,描述像素图的(最大)最终大小。
还有两个可选参数。aspectRatioMode
处理长宽比。这文档详细介绍了不同的选项及其效果。transformMode
定义缩放的方式(哪种算法)。它可能会改变图像的最终质量。你可能不需要这个。
所以,把它们放在一起你应该有(Qt
命名空间在里面QtCore
):
# substitute the width and height to desired values
self.myLabel.setPixmap(QtGui.QPixmap(_fromUtf8(directory + '\\' + tempName)).scaled(width, height, QtCore.Qt.KeepAspectRatio))
或者,如果您有固定尺寸QLabel
,你可以调用.size()
从中获取尺寸的方法:
self.myLabel.setPixmap(QtGui.QPixmap(_fromUtf8(directory + '\\' + tempName)).scaled(self.myLabel.size(), QtCore.Qt.KeepAspectRatio))
Note: 你可能想用os.path.join(directory, tempName)
为了directory + '\\' + tempName
part.