Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Strange QDataStream & QByteArrray
Forum Update on Tuesday, May 27th 2025

Strange QDataStream & QByteArrray

Scheduled Pinned Locked Moved General and Desktop
14 Posts 3 Posters 5.2k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    mmmaaak
    wrote on last edited by
    #1

    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??

    1 Reply Last reply
    0
    • C Offline
      C Offline
      cincirin
      wrote on last edited by
      #2

      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)

      1 Reply Last reply
      0
      • M Offline
        M Offline
        mmmaaak
        wrote on last edited by
        #3

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

        PS: I work with linux and Qt3

        1 Reply Last reply
        0
        • C Offline
          C Offline
          cincirin
          wrote on last edited by
          #4

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

          1 Reply Last reply
          0
          • C Offline
            C Offline
            cincirin
            wrote on last edited by
            #5

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

            1 Reply Last reply
            0
            • M Offline
              M Offline
              mmmaaak
              wrote on last edited by
              #6

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

              1 Reply Last reply
              0
              • M Offline
                M Offline
                mmmaaak
                wrote on last edited by
                #7

                barray.size() is 170

                1 Reply Last reply
                0
                • M Offline
                  M Offline
                  mmmaaak
                  wrote on last edited by
                  #8

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

                  1 Reply Last reply
                  0
                  • C Offline
                    C Offline
                    cincirin
                    wrote on last edited by
                    #9

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

                    1 Reply Last reply
                    0
                    • M Offline
                      M Offline
                      mmmaaak
                      wrote on last edited by
                      #10

                      Any Ideas for calculating this offset?

                      1 Reply Last reply
                      0
                      • C Offline
                        C Offline
                        cincirin
                        wrote on last edited by
                        #11

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

                        1 Reply Last reply
                        0
                        • T Offline
                          T Offline
                          tobias.hunger
                          wrote on last edited by
                          #12

                          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.

                          1 Reply Last reply
                          0
                          • M Offline
                            M Offline
                            mmmaaak
                            wrote on last edited by
                            #13

                            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?

                            1 Reply Last reply
                            0
                            • T Offline
                              T Offline
                              tobias.hunger
                              wrote on last edited by
                              #14

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

                              1 Reply Last reply
                              0

                              • Login

                              • Login or register to search.
                              • First post
                                Last post
                              0
                              • Categories
                              • Recent
                              • Tags
                              • Popular
                              • Users
                              • Groups
                              • Search
                              • Get Qt Extensions
                              • Unsolved