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. From QDataStream to QDataStream (Merging streams)
Forum Updated to NodeBB v4.3 + New Features

From QDataStream to QDataStream (Merging streams)

Scheduled Pinned Locked Moved General and Desktop
2 Posts 2 Posters 1.9k 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.
  • S Offline
    S Offline
    Seraph
    wrote on last edited by
    #1

    Hi fellas,

    Is it somehow possible to write from a datastream into a datastream!?
    So to say: I want to "merge" two QDataStreams!

    Something like this:
    @QByteArray b;

    QDataStream out(&b, QIODevice::ReadWrite);

    //serialize some data to b
    out<<(int)2;
    out<<(int)3;
    out<<(int)4;

    //message will contain some aditional info + the serialized data
    QByteArray message;
    QDataStream msgOut(&message, QIODevice::WriteOnly);

    msgOut<<(int)1;
    out.device()->seek(0);
    msgOut<<out; //write serialized to the stream

    //now try to deserialize from message
    QDataStream msgIn(&message, QIODevice::ReadOnly);

    int var1;
    int var2;
    int var3;
    int var4;

    msgIn>>var1;
    msgIn>>var2;
    msgIn>>var3;
    msgIn>>var4;

    std::cout<<"Var1: "<<var1<<std::endl; //contains 1 (correct)
    std::cout<<"Var2: "<<var2<<std::endl; //contains 2 (correct)
    std::cout<<"Var3: "<<var3<<std::endl; //contains 3 (correct)
    std::cout<<"Var4: "<<var4<<std::endl; //contains some bullsh@

    Some values are allways missing. Mostly it is exactly the last value!
    Do I have to add something?

    A working but slow solution is:
    @
    QByteArray b;
    QDataStream out(&b, QIODevice::ReadWrite);

    //serialize some data to b
    out<<(int)2;
    out<<(int)3;
    out<<(int)4;

    //message will contain some aditional info + the serialized data
    QByteArray message;
    QDataStream msgOut(&message, QIODevice::WriteOnly);

    msgOut<<(int)1;
    msgOut<<b; //here is the difference! Writing b insteat of out

    //now try to deserialize from message
    QDataStream msgIn(&message, QIODevice::ReadOnly);

    int var1;
    int var2;
    int var3;
    int var4;

    b.clear();

    msgIn>>var1;
    msgIn>>b; //i have to deserialize b first! That is a circumstance!

    QDataStream in(&b, QIODevice::ReadOnly);
    in>>var2;
    in>>var3;
    in>>var4;

    std::cout<<"Var1: "<<var1<<std::endl; //contains 1 (correct)
    std::cout<<"Var2: "<<var2<<std::endl; //contains 2 (correct)
    std::cout<<"Var3: "<<var3<<std::endl; //contains 3 (correct)
    std::cout<<"Var4: "<<var4<<std::endl; //contains 4 (correct)
    @

    It is working fine, but i think it costs performance! Because it first writes all bytes back to b!!!

    I hope everyone got the problem!?

    Thanks in advance

    1 Reply Last reply
    0
    • R Offline
      R Offline
      rcari
      wrote on last edited by
      #2

      Doing seek(0) you explicitely tell you want to write at the beginning of the underlying QIODevice, aka the very beginning of the QByteArray. You have a construction problem. If I understand correctly you are trying to add some sort of header in front of your serialized data. If you want to do that, and that the header contents depends on the serialized data, I see no other solution but to serialize the data in memory using a QByteArray, computing your header, writing it to the output stream and then writing the QByteArray to the stream.

      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