[PyQt] Help with "stringio" ops on QTextStream in Qt5

David Cortesi davecortesi at gmail.com
Sat Feb 15 18:18:33 GMT 2014


I asked about basing an in-memory QTextStream on a QString
when QStrings are not available, and Colin replied,

Would using QByteArray in place of QString be acceptable?
>
> It seems to accept and return Python strings rather than insist on
> Python bytes and seems to cope with multi-byte characters.  However,
> since byte offset isn't equal to character offset in the case of
> multibyte characters I suppose seeking to arbitrary character positions
> would be tricky.
>

Thanks for the reply. You are correct, using QByteArray works just fine
for this. I pushed a bunch of multi-byte chars into it and got them
all back on read, Python string in, Python string out.

Your point about seeking to a character position is cogent, but
fortunately I only care about using seek(0) to "rewind" the stream.

It is possible to set the encoding of a QTextStream, e.g.

>   stream.setCodec(QTextCodec.codecForName('UTF-8'))

but this doesn't seem to be necessary, at least in Mac OS.
However, maybe it should be done to ensure consistency.
The default codec is supposedly QTextCodec.codecForLocale()
I'm not sure whether this might cause a change of
behavior when moving the app to different platforms.

Thanks again for the help!
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.riverbankcomputing.com/pipermail/pyqt/attachments/20140215/25ce0d21/attachment.html>


More information about the PyQt mailing list