[PyQt] PyQt4 for Python 3

Mark Summerfield list at qtrac.plus.com
Sat Jun 6 08:36:44 BST 2009


Hi,

I've started converting the examples for "Rapid GUI Programming with
Python and Qt" to Python 3. I've only hit two problems, one of which I
think is a PyQt bug.

(1) Bug?
    Every .ui file converted by pyuic4 gives me an error message like
    this:
    $ ./imagechanger.pyw 
Traceback (most recent call last):
  File "./imagechanger.pyw", line 264, in fileNew
    dialog = newimagedlg.NewImageDlg(self)
  File "/home/mark/books/rapid-gui-programming-with-python-and-
qt/eg3/chap06/newimagedlg.py", line 22, in __init__
    self.setupUi(self)
  File "/home/mark/books/rapid-gui-programming-with-python-and-
qt/eg3/chap06/ui_newimagedlg.py", line 71, in setupUi
    self.retranslateUi(NewImageDlg)
AttributeError: 'NewImageDlg' object has no attribute 'retranslateUi'

    In Python 2, pyuic4 adds the retranslateUi() method to the Ui_*
    class, but this is not happening for Python 3.

    Python 3.0.1 Qt 4.5.1 PyQt 4.5 on Linux

(2) Behaviour change.
    QImageReader.supportedImageFormats() and
    QImageWriter.supportedImageFormats() both return a list of
    QByteArrays. With Python 2 you can convert each QByteArray to a
    unicode string like this:
	text = unicode(byte_array)
    If you change this for Python 3 version to:
	text = str(byte_array) # str is Unicode in Python 3
    it will "work", but the string you'll get back will be the repr,
    e.g., "PyQt4.QtCore.QByteArray(b'bmp').
    For Python 3 I'm now doing it like this:
	text = byte_array.data().decode("ascii") # text == "bmp"
    I'm not saying this is the best way, just a way that works.
	
Other than that I found the conversion pretty straightforward.
I'll put the Python 3 versions on my website soon, hopefully next week.

-- 
Mark Summerfield, Qtrac Ltd, www.qtrac.eu
    C++, Python, Qt, PyQt - training and consultancy
        "Programming in Python 3" - ISBN 0137129297




More information about the PyQt mailing list