[PyQt] Converting to API 2

detlev detlev at die-offenbachs.de
Thu Apr 22 18:28:30 BST 2010


On Donnerstag, 22. April 2010, Mark Summerfield wrote:
> Hi,
> 
> I'm starting to convert the examples from my PyQt book to Python 3 with
> API 2. Perhaps I'm not awake yet, but I'm puzzled by this:
> 
>     Python 3.1.2 (r312:79147, Mar 25 2010, 07:42:02)
>     [GCC 4.4.1] on linux2
>     Type "copyright", "credits" or "license()" for more information.
> 
>     >>> from PyQt4.QtCore import *
>     >>> from PyQt4.QtGui import *
>     >>> PYQT_VERSION_STR
> 
>     '4.7.3'
> 
>     >>> QT_VERSION_STR
> 
>     '4.6.1'
> 
>     >>> fh = QFile("/tmp/data")
>     >>> fh.open(QIODevice.WriteOnly)
> 
>     True
> 
>     >>> out = QDataStream(fh)
>     >>> out.writeString("hello")
> 
>     Traceback (most recent call last):
>     File "<pyshell#5>", line 1, in <module>
> 	out.writeString("hello")
>     TypeError: QDataStream.writeString(str): argument 1 has unexpected type
> 'str'

This should be

>>> out.writeString("hello".encode())

i.e. it must be converted to a bytes object.


> 
> And also by this:
>     >>> fh = QFile("/tmp/data")
>     >>> fh.open(QIODevice.WriteOnly)
> 
>     True
> 
>     >>> out = QDataStream(fh)
>     >>> out << "hello"

You should use writeString(). When I ported eric to Python3, I stumbled over 
both these issues.

> 
>     Traceback (most recent call last):
>     File "<pyshell#11>", line 1, in <module>
> 	out << "hello"
>     ValueError: string of length 1 expected
> 
> What am I missing here?
> 

Detlev
-- 
Detlev Offenbach
detlev at die-offenbachs.de


More information about the PyQt mailing list