2012-02-19 182 views
2

我正在编写一个程序,允许我通过他们的API上传照片到TUMBLR,我有上传工作(感谢你们)。QPixmap保持长宽比

我在GUI的一侧放置了一个'queueBox',它显示图像名称,并且它们存储在QListWidget中。我已经把这个在我的主类的构造函数:

def __init__(self): 
    QtGui.QMainWindow.__init__(self) 
    self.setupUi(self) 
    self.queueBox.itemClicked.connect(self.displayPhoto) 

,我有这样的方法:

def displayPhoto(self, item): 
    tempName = (item.text()) 
    print tempName 
    self.myLabel.setPixmap(QtGui.QPixmap(_fromUtf8(directory + '\\' + tempName))) 
    ## self.myLabel.pixmap(QPixmap.scaled(aspectRatioMode = Qt.IgnoreAspectRatio)) 
    ##^^^What do I do with this? How do I set it to maintain aspect ratio? 
    ## Currently it says ''NameError: global name 'Qt' is not defined'' 

这成功地借鉴了图像myLabel这是一个QLabel,然而,这是非常缩放,我有

self.myLabel.setScaledContents(True) 

在我ui_mainWindow类,如果我把它转化为False,它修复了缩放,但它只能显示图像的一小部分,因为图像比QLabel大得多。我想要的是能够保持高宽比,所以它看起来不会缩小和可怕。

我发现这个:http://www.riverbankcomputing.co.uk/static/Docs/PyQt4/html/qpixmap.html 它说如何使用它,但我不能让它的工作,如上面的代码在我的评论中所示。有谁知道如何使用它?如果是这样,你能否给我提供一个例子,我尝试过搜索,但是我得到的大部分结果都是在C++中运行的例子,而不是python。

谢谢!

回答

7

摆脱

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,所以它返回原始像素图的缩放副本

然后你需要一个width和一个height,描述像素图的(最大)最终尺寸。

另外两个可选参数。 aspectRatioMode处理好的纵横比。 documentation详细介绍了不同的选项及其效果。 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)) 

注意:您可能需要使用os.path.join(directory, tempName)directory + '\\' + tempName一部分。

+1

好的,非常感谢你,这很好。我修改了一下: 'self.myLabel.setPixmap(QtGui.QPixmap(_fromUtf8(directory +'\\'+ tempName))。scaled(self.myLabel.size(),QtCore.Qt.KeepAspectRatio,QtCore.Qt .SmoothTransformation))'我添加了“smoothTransformation”,因为它使图像适应标签的质量要好得多。然而,当图像被创建时,它位于标签的左侧,即如果它是一个正方形,它不会位于标签的中间,它将位于左侧,我搜索了文档并找不到什么来执行我所追求的任何想法? – Anteara 2012-02-20 05:24:16

+0

哦,我在上面没有很多信息。我想要做的是在标签中间有一个正方形图像,而不是左侧,所以它看起来与窗口的其余部分成正比。我会告诉你一个我的意思的例子:http://i.imgur.com/0nQWU.jpg 正如你所看到的方形图像位于左侧,而不是中间,宽屏图像是好的。 – Anteara 2012-02-20 05:33:49

+0

@Anteara:这取决于'QLabel'。只需通过Designer(如果您通过Designer设计UI)或调用'self.myLabel.setAlignment(QtCore.Qt.AlignCenter)'来设置它的对齐方式。 – Avaris 2012-02-20 06:52:36

0

PyQt5代码更改更新:

avaris以上回答需要PyQt5更新,因为它打破。

QPixmap.scaled (self, int width, int height, Qt.AspectRatioMode aspectRatioMode = Qt.IgnoreAspectRatio 

保持在低于追踪错误的代码的结果self

TypeError: arguments did not match any overloaded call: scaled(self, int, int, aspectRatioMode: Qt.AspectRatioMode = Qt.IgnoreAspectRatio, transformMode: Qt.TransformationMode = Qt.FastTransformation): argument 1 has unexpected type 'MainUI' scaled(self, QSize, aspectRatioMode: Qt.AspectRatioMode = Qt.IgnoreAspectRatio, transformMode: Qt.TransformationMode = Qt.FastTransformation): argument 1 has unexpected type 'MainUI'

因此这应该是(没有 “自我”, “Qt的”)作为以下陈述:

QPixmap.scaled (int width, int height, aspectRatioMode = IgnoreAspectRatio 

或:

QPixmap.scaled (int width, int height, aspectRatioMode = 0) 

KeepAspectRatio = 4 ...但所用由上述代码中的aspectRatioMode = 4提供。请享用!