Alex
Alex

Reputation: 44305

PyQT4 and QPixmap: load image with size zero?

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.

Upvotes: 1

Views: 1126

Answers (1)

ekhumoro
ekhumoro

Reputation: 120598

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).

Upvotes: 3

Related Questions