[PyQt] Converting to API 2
Mark Summerfield
list at qtrac.plus.com
Fri Apr 23 08:16:50 BST 2010
On 2010-04-22, detlev wrote:
> 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.
Ah, thank you!
So basically, the docs which say QDataStream.writeString(str) assume
that str is a Python 2 string not a Python 3 string even for API 2:-(
> > 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.
Oh, sure, I just showed that because it looked like the same problem.
Thanks!
--
Mark Summerfield, Qtrac Ltd, www.qtrac.eu
C++, Python, Qt, PyQt - training and consultancy
"C++ GUI Programming with Qt 4" - ISBN 0132354160
More information about the PyQt
mailing list