Strange QDataStream & QByteArrray
-
I'm using Qt3 and trying to write into byte array some information^
@
QByteArray barray;
QDataStream stream(barray,IO_ReadWrite);
stream.setVersion(6);
stream<<"some info here";
std::cout<<"ByteArray: "<<barray.data()<<"\n";
@When I'm trying to output my bytearray its empty??? Whats wrong??
-
I see nothing wile outputing my data. Maybe some data which I write to my stream is incorrect?
There my data:
@
QString data("a=5&b=qwert");
stream<<"POST /index.php HTTP/1.1\r\n"<<"Content-Length: "<<data.length()<<"\r\n"<<"Content-type: application/x-www-form-urlencoded\r\n\r\n"<<data<<"\r\n";
@ -
[quote author="mmmaaak" date="1330518203"]I'm using Qt3 and trying to write into byte array some information^
@
QByteArray barray;
QDataStream stream(barray,IO_ReadWrite);
stream.setVersion(6);
stream<<"some info here";
@
[/quote]What is the size of this barray ? :-) I have tested with this code ...
-
The QByteArray is just the raw data you add into it. But you need to be aware that QDataStream adds meta information into the stream to allow the other side to decode the data again.
This meta information includes the version number, which you should not usually set: QDataStream will use it to decide how to de-/encode the input. setVersion is provided so that you can have compatibility with older Qt versions. You should use on of the Version enum found in QDataStream to set it correctly.
-
No, QDataStream is meant to exchange data between different machines in a save way, so this meta-information is needed.
You could use QBuffer instead though, that should do what you want.
You want to make sure to set the Content-Length to the actual number of bytes sent, not the number of letters in your string. With unicode those can differ (though you should be save when using ASCII).
Also make sure to store a string representation of the number there, not the plain int value (which QDataStream and QBuffer both do).