stream.writeQString adds characters in the beginning
-
Hello!
stream.writeQString is adding some characters in front of my string.
\x00\x00\x00$
why is this happening? can this be removed? I am trying to send some text via TCP and this is messing up the comunication for me.
my code:
def sendResult(self, id, crate, program, folddir): SIZEOF_UINT16 = 2 reply = QByteArray() stream = QDataStream(reply, QIODevice.WriteOnly) stream.setVersion(QDataStream.Qt_4_2) stream.writeQString(id + "," + crate + "," + program + "," + folddir) stream.device().seek(0) stream.writeUInt16(reply.size() - SIZEOF_UINT16) self.socket.write(reply)
the blue part is the part that is in eccess..
thank you, any help appreciated!
-
@Igor86 said in stream.writeQString adds characters in the beginning:
QDataStream
it writes some additional data into the stream to be able to read the data from the stream later properly.
If you use QDataStream to write you also should use it to read the data.
Else do not use QDataStream. -
@jsulm said in stream.writeQString adds characters in the beginning:
@Igor86 said in stream.writeQString adds characters in the beginning:
QDataStream
it writes some additional data into the stream to be able to read the data from the stream later properly.
If you use QDataStream to write you also should use it to read the data.
Else do not use QDataStream.Thank you for your answer, but it is somewhat confusing me. I started coding with python and pyqt5 one week ago, so please bear with me. I found this code in a sample and adapted it for my needs. What should I be using instead of QDataStream to send plain text to a different pc?
-
-
@JonB thank you!
Like this it worked:
out = id + "," + crate + "," + program + "," + folddir self.socket.write(bytearray(out, 'ascii'))
would you mind please explaining for future reference in what situations I would use QDataStream and what the advantages are?
thank you very much!
-
@Igor86
As @jsulm said,QDataStream
is only of use if you have 2 Qt programs as sender & receiver. Otherwise you can't use it.If you do, it provides convenient methods to exchange typed data, not just bytes. It works for integers (cross-platform), strings, lists, .... This is very convenient, but is why there is "extra" information when you look at what it produces. It is opaque to you: it works, but you don't understand its data format.
It also provides support for utilities like buffering its output and "transactions" to recognise when data has been completely transferred. Though you can write those yourself if you need them outside of
QDataStream
.When you can use it it is "convenient" and "efficient" and "portable". But you can't, because your other end is not using it. You want to exchange just plain text, so you do that yourself without
QDataStream
's layer -
@JonB said in stream.writeQString adds characters in the beginning:
@Igor86
As @jsulm said,QDataStream
is only of use if you have 2 Qt programs as sender & receiver. Otherwise you can't use it.If you do, it provides convenient methods to exchange typed data, not just bytes. It works for integers (cross-platform), strings, lists, .... This is very convenient, but is why there is "extra" information when you look at what it produces. It is opaque to you: it works, but you don't understand its data format.
It also provides support for utilities like buffering its output and "transactions" to recognise when data has been completely transferred. Though you can write those yourself if you need them outside of
QDataStream
.When you can use it it is "convenient" and "efficient" and "portable". But you can't, because your other end is not using it. You want to exchange just plain text, so you do that yourself without
QDataStream
's layervery clear! tank you for taking your time to explain!