QPixmap 保持纵横比
我正在编写一个程序,允许我通过他们的 API 将照片上传到 TUMBLR,我已经可以正常上传了(谢谢你们)。
我在 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。
谢谢!
I'm writing a program that will allow me to upload photos to TUMBLR via their API, I've got the uploading working (thanks to you guys).
I've put a 'queueBox' on the side of the GUI, which displays the image names, and they are stored in a QListWidget. I've put this in my Main Class' constructor:
def __init__(self):
QtGui.QMainWindow.__init__(self)
self.setupUi(self)
self.queueBox.itemClicked.connect(self.displayPhoto)
and I have this method:
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''
This sucessfully draws the image on to myLabel which is a QLabel, however, It is very scaled, I have
self.myLabel.setScaledContents(True)
in my ui_mainWindow class, and if I turn it to False, it fixes the scaling but it only shows a small portion of the image because the image is much larger than the QLabel. What I want is to be able to maintain the aspect ratio, so it doesn't look scaled and horrible.
I found this: http://www.riverbankcomputing.co.uk/static/Docs/PyQt4/html/qpixmap.html
and it says how to use it, however I can't get it to work as shown in the code above in my comments. Does anyone know how to use this? If so, can you provide me with an example, I've tried searching but most of the results I get are working examples in C++, not python.
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
摆脱
呼叫(或将其设置为 False)。它用像素图填充您的小部件,而不关心宽高比。
如果您需要调整
QPixmap
的大小,正如您所发现的,scaled
是必需的方法。但你调用它是错误的。让我们看一下定义:该函数的返回类型是
QPixmap
,因此它返回原始像素图的缩放副本。然后,您需要一个
宽度
和一个高度
,描述像素图的(最大)最终尺寸。还有两个可选参数。
aspectRatioMode
处理长宽比。 文档详细介绍了不同的选项及其效果。transformMode
定义缩放的方式(哪种算法)。它可能会改变图像的最终质量。你可能不需要这个。因此,将它们放在一起,您应该拥有(
Qt
命名空间位于QtCore
内部):或者,如果您有固定大小的
QLabel
,您可以调用.size()
方法从中获取大小:注意:您可能需要使用
os.path.join(directory, tempName)
对于目录 + '\\' + tempName
部分。Get rid of the
call (or set it to False). It is filling your widget with the pixmap without caring about the aspect ratio.
If you need to resize a
QPixmap
, as you have found,scaled
is the required method. But you are invoking it wrong. Let's look at the definition:Return type of this function is
QPixmap
, so it returns a scaled copy of the original pixmap.Then you need a
width
and aheight
, describing the (maximum) final size of the pixmap.Two more optional parameters.
aspectRatioMode
deals with the, well aspect ratio. The documentation details the different options and their effects.transformMode
defines how (which algorithm) the scaling is done. It might change the final quality of your image. You probably don't need this one.So, putting it together you should have (
Qt
namespace is insideQtCore
):Alternatively, if you have a fixed size
QLabel
, you could call the.size()
method to get the size from it:Note: You might want to use
os.path.join(directory, tempName)
for thedirectory + '\\' + tempName
part.PyQt5 代码更改更新:
avaris 的上述答案需要 PyQt5 更新,因为它损坏了。
将
self
保留在代码中会导致以下回溯错误。因此这应该是(没有“self”,“Qt”)如下所述:
或:
KeepAspectRatio = 2...但是按照上述代码中的
aspectRatioMode = 2
提供的方式使用。享受!PyQt5 code change update:
The above answer of avaris needed a PyQt5 update because it breaks.
Keeping the
self
in the code results in below traceback error.Thus this should be (without "self", "Qt") as stated below:
or:
KeepAspectRatio = 2... but used as provided by
aspectRatioMode = 2
in above code. Enjoy!