PyQt:如何正确显示图像? [英] PyQt:How do I display a image properly?
问题描述
我想在 pyqt 中显示图像,所以,我使用了标签和像素图选项,以及缩放内容,但图像失真.我应该使用其他小部件还是做其他事情?谢谢.
I want to display an image in pyqt so,i used a label and the pixmap option,and the scaledContents but the image is distorted.Should I use another widget or do something else? Thanks.
这是代码:
from PyQt4 import QtCore, QtGui
self.label.setPixmap(QtGui.QPixmap(_fromUtf8('image.jpg')))
self.label.setScaledContents(True)
我使用 QtDesigner.
I use QtDesigner.
我试过这个:self.label.pixmap().scaled(QtCore.QSize(self.label.size()), QtCore.Qt.KeepAspectRatio, QtCore.Qt.FastTransformation)
但图像不会调整大小以适合标签.
but the images don't get resized to fit the label.
推荐答案
使用像素图的scaled(const QSize, Qt::AspectRatioMode, Qt::TransformationMode)
方法,它有一个选项Qt::KeepAspectRatio
不会使图像变形.默认是忽略纵横比
Use the scaled(const QSize, Qt::AspectRatioMode, Qt::TransformationMode)
method of the pixmap, it has an option Qt::KeepAspectRatio
that does not deform the image. The default is to ignore the aspect ratio
另外,注意scaled
方法返回缩放后的pixmap
,所以必须这样使用:
Also, note that the scaled
method returns the scaled pixmap
, so it must be used this way:
myPixmap = QtGui.QPixmap(_fromUtf8('image.jpg'))
myScaledPixmap = myPixmap.scaled(self.label.size(), Qt.KeepAspectRatio)
self.label.setPixmap(myScaledPixmap)
这篇关于PyQt:如何正确显示图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!