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. QDataStream and QByteArray as a buffer
Forum Updated to NodeBB v4.3 + New Features

QDataStream and QByteArray as a buffer

Scheduled Pinned Locked Moved General and Desktop
5 Posts 2 Posters 16.4k 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.
  • P Offline
    P Offline
    p-himik
    wrote on last edited by
    #1

    Can you explain why this code:
    @int main(int argc, char *argv[])
    {
    QByteArray buffer;
    QHash< QString, QByteArray > data;
    data.insert( "123", "qwe" );
    qDebug() << "Data:" << data;
    QDataStream stream( &buffer, QIODevice::ReadWrite );
    stream << data;
    qDebug() << "Buffer's size:" << buffer.size();
    data.clear();
    stream >> data;
    qDebug() << "Data:" << data;
    qDebug() << "Buffer's size:" << buffer.size();
    qDebug() << "Buffer:" << buffer.toHex();
    }@
    gives this output:
    @Data: QHash(("123", "qwe"))
    Buffer's size: 21
    Data: QHash()
    Buffer's size: 21
    Buffer: "000000010000000600310032003300000003717765" @

    As i can understand from http://doc.qt.nokia.com/stable/datastreamformat.html streaming from data to buffer is correct (the last line of the output). But why then there is no streaming from buffer to data?

    1 Reply Last reply
    0
    • P Offline
      P Offline
      p-himik
      wrote on last edited by
      #2

      Stream's status becomes QDataStream::ReadPastEnd so i think i just misunderstood logic of streaming.

      1 Reply Last reply
      0
      • D Offline
        D Offline
        dangelog
        wrote on last edited by
        #3

        Indeed. You have to seek() back to the beginning of the buffer.

        Software Engineer
        KDAB (UK) Ltd., a KDAB Group company

        1 Reply Last reply
        0
        • P Offline
          P Offline
          p-himik
          wrote on last edited by
          #4

          Do you mean QDataStream::device().seek()?

          1 Reply Last reply
          0
          • D Offline
            D Offline
            dangelog
            wrote on last edited by
            #5

            Yes (remember that QByteArray isn't a QIODevice, thus QDataStream creates a QBuffer internally).

            Software Engineer
            KDAB (UK) Ltd., a KDAB Group company

            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