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 find nothing in documentation but if you write :
    @
    std::cout<<"ByteArray: "<< barray.data() + 4 <<"\n";
    @
    you get what you want.

    (maybe barray is prefixed with some 32 bits value)



  • Are you sure in this code?? I tryed to use it and trere is no any effect.

    PS: I work with linux and Qt3



  • Yes I'm sure :-)
    I tried with Qt3 on windows. The only difference is I used IO_WriteOnly instead of IO_ReadWrite



  • btw ... What's the size of barray ? On my end is 19 and "some info here" string point to &(barray [4] )



  • 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";
    @



  • barray.size() is 170



  • As I understand such offset appear for keeping size of this bytaArray?



  • [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 ...



  • Any Ideas for calculating this offset?



  • I have not found anything relevant in the documentation about this offset ...


  • Moderators

    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.



  • Can I make QDataSctream not to encode data and not to include this meta-data? Isend my data to web-server which cant decode it?


  • Moderators

    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).


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.