PyQT4 和 QPixmap:加载大小为零的图像? [英] PyQT4 and QPixmap: load image with size zero?

查看:55
本文介绍了PyQT4 和 QPixmap:加载大小为零的图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对 PyQt4 感到困惑.我在python2.6上尝试了以下步骤:

In [1]: from PyQt4 import QtGui在[2]中:导入系统在 [3]: app = QtGui.QApplication(sys.argv)在[4]中:pix = QtGui.QPixmap("P1010001.JPG")在 [5] 中:pix.width(), pix.height()输出[5]:(0, 0)

为什么widthheight 显示为零?图像存在并且很好.这完全违反直觉,我不希望 Python 能做到这一点.

解决方案

PyQt 在这里和那里添加了一些语法糖,使事情更加 Pythonic.但它主要是围绕 Qt(它是一个 C++ 库)的一个相当薄的包装器 - 因此,期望 PyQt 总是 以 Python 程序员直观的方式运行是错误的.>

我想大多数 Python 程序员可能希望 QPixmap 在设置不存在的路径时引发错误.但是 Qt 不会这样做,在这种情况下,PyQt 也不会.相反,您可以使用以下方法检查您是否拥有有效的像素图:

pix.isNull()

要实际修复示例中的代码,您显然必须先更改到适当的目录(或使用绝对路径).

I am confused by PyQt4. I have tried the following steps on python2.6:

In [1]: from PyQt4 import QtGui

In [2]: import sys

In [3]: app = QtGui.QApplication(sys.argv)

In [4]: pix = QtGui.QPixmap("P1010001.JPG")

In [5]: pix.width(), pix.height()
Out[5]: (0, 0)

Why does width and height show zero? The image exists and is fine. This is completely counterintuitive, which I do not expect from python.

解决方案

PyQt adds a little syntactic sugar here and there to make things more Pythonic. But it is mostly a fairly thin wrapper around Qt (which is a C++ library) - and so it would be a mistake to expect PyQt to always behave in a way that is intuitive to Python programmers.

I suppose most Python programmers might expect QPixmap to raise an error when setting a path that doesn't exist. But Qt doesn't do this, and, in this case, neither does PyQt. Instead, you can check that you have a valid pixmap by using:

pix.isNull()

To actually fix the code in your example, you will obviously have to change to the appropriate directory first (or use an absolute path).

这篇关于PyQT4 和 QPixmap:加载大小为零的图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆