[PyQt] Help with "stringio" ops on QTextStream in Qt5
Colin McPhail
colin.mcphail at mac.com
Fri Feb 14 17:47:47 GMT 2014
On 2014-02-13, David Cortesi <davecortesi at gmail.com> wrote:
> I am upgrading an app from PyQt4 to PyQt5.
> This app makes frequent use of in-memory i/o by
> creating a QTextStream based on a QString, e.g.:
>
>> string = QString() # must stay in scope!
>> stream = QTextStream(string)
>> stream << "whatever, yadda yadda"
>> # in another part of the forest...
>> stream.seek(0)
>> while not stream.atEnd():
>> foobar = stream.readLine()
>
> How to make this work in the new API sans QStrings?
> QTextStream(python-string-var) not unexpectedly
> yields an error. Do I need to recode all this to use
> Python's stringIO class, or is there a way to preserve it?
>
> Thanks for your suggestions,
> Dave Cortesi
Would using QByteArray in place of QString be acceptable?
>>> string = QByteArray()
>>> stream = QTextStream(string)
>>> stream << "whatever, yadda yadda\n"
>>> stream << "ß∂©\n"
>>> stream.seek(0)
>>> while not stream.atEnd():
... foobar=stream.readLine()
... print(foobar)
...
whatever, yadda yadda
ß∂©
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.
-- Colin
More information about the PyQt
mailing list