[PyQt] UnicodeDecodeError with output from Windows OS command

Florian Bruhin me at the-compiler.org
Thu Nov 30 22:03:51 GMT 2017

On Thu, Nov 30, 2017 at 09:49:33PM +0000, J Barchan wrote:
> I am being told elsewhere by a Qt expert that all I need to write in C++
> would be:
> myTextEdit->setText(myProcess->readAllStandardOutput());
> That's it!  Which looks perfect to me.

This is the implicit version of QString being called with a QByteArray as
argument, which, according to the docs, decodes it as UTF-8.

> Now, please, how can I do that from PyQt?  The C++ is somehow passing a
> QByteArray as a QString, but if I understand correctly I cannot use those
> types with PyQt.
> Instead I must use Python's `bytes` & `str` types respectively, and then I
> end up have to call `decode()` and specify an encoding, and that's where I
> get lost.

Well, the equivalent to what Qt does is passing 'utf-8' as encoding and
'replace' as error handler:

  bytearray.decode('utf-8', 'replace')

Like explained in my earlier mail, you'll end up with something like question
marks instead of those correct characters, just like you'd with the Qt solution

Just because a solution doesn't raise an exception (which Qt doesn't do in
general) it doesn't mean it's correct or the right thing to do.

The right thing to do would be passing the correct encoding, but I'm not going
to repeat myself again ;-)


https://www.qutebrowser.org  | me at the-compiler.org (Mail/XMPP)
   GPG: 916E B0C8 FD55 A072  | https://the-compiler.org/pubkey.asc
         I love long mails!  | https://email.is-not-s.ms/
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 833 bytes
Desc: not available
URL: <https://www.riverbankcomputing.com/pipermail/pyqt/attachments/20171130/ce6f2730/attachment.sig>

More information about the PyQt mailing list