[PyQt] QString, QTextEdit and Encoding problem

Andreas Pakulat apaku at gmx.de
Tue Jun 28 06:29:41 BST 2011


On 28.06.11 06:26:24, Yaşar Arabacı wrote:
> Hi,
> 
> I am havin an encoding problem. If you have read my earlier post, I was
> doing a simple chat application. Here is how it goes.

You're not converting your data correctly. On the sending side you do it
properly, decoding to utf-8 and then sending the bytes through the
network. On the receiving side you're using Python's socket API, which
gives you a byte-stream and then create a QString from it implicitly.
That uses whatever your systems default-encoding is to decode the bytes
into a string.

The error is in NetConnectorWorker.run, but its not that easy to fix
since you currently do not send the amount of bytes you cannot know on
the receiving side wether all data has been received or not and hence
you cannot do a correct decoding.

I suggest to switch to use Qt's network stack, that'll make these things
a bit easier since you can use QDataStream on the network socket which
handles sending QString's just fine.

If thats not possible, you'll have to add the length of the message into
the byte-stream or always read up to the first \n and decode the content
then and make sure the sender always sends at least 1 \n.

Andreas



More information about the PyQt mailing list