[PyQt] Corrupted data in QDataStream

Nick Gaens nickgaens at gmail.com
Wed Dec 2 11:31:51 GMT 2009


Hello everyone,

I'm trying to send a python-string via a QTcpSocket. I am using this
structure:

----------------------------------------
block = QtCore.QByteArray()
stream = QtCore.QTextStream(block, QtCore.QIODevice.WriteOnly)
stream << myPythonString

if myTcpSocket.isWritable():
    myTcpSocket.write(block)
-----------------------------------------

However, it now seems that there are some corrupted characters at the
beginning of the bytearray (or textstream) :-S..

A solution for this can be to call stream.flush() just before I stream
myPythonString to it..
In code:

----------------------------------------
block = QtCore.QByteArray()
stream = QtCore.QTextStream(block, QtCore.QIODevice.WriteOnly)
stream.flush() # removes the ascii dump successfully
stream << myPythonString

if myTcpSocket.isWritable():
    print "writing data to ", str(socket.peerAddress().toString())
    myTcpSocket.write(block) # crashes here
-----------------------------------------

But at this point, the program crashes at the last line when actually
writing to the socket.. How come? I found out that this crash can be
reproduced anywhere when calling stream.string() just after streaming
myPythonString to the textstream..
In code:

----------------------------------------
block = QtCore.QByteArray()
stream = QtCore.QTextStream(block, QtCore.QIODevice.WriteOnly)
stream.flush() # removes the ascii dump
stream << myPythonString
print str(stream.string()) # crashes here already..

if myTcpSocket.isWritable():
    print "writing data to ", str(socket.peerAddress().toString())
    myTcpSocket.write(block)
-----------------------------------------

How on earth can this be?

-- 
Nick Gaens
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.riverbankcomputing.com/pipermail/pyqt/attachments/20091202/16093212/attachment.html


More information about the PyQt mailing list