2

i have byte image array i want to write this byte array to another byte array with add some another value on second byte array , i'm using this code but i think something is wrong

 QByteArray byteArray;

 QDataStream ds(&byteArray,QIODevice::ReadWrite);

 ds<<(qint32)20;

 ds<<bArray;

 qint32 code;

 ds>>code;

when i trace ds>>code it is always have 0 value but in fact it must have 20 value and i used ds.resetStatus(); but it return 0 value again

NetProjects Ir
  • 61
  • 2
  • 10

1 Answers1

2

I suspect that QDataStream::operator<< functions sets some sort of pointer/iterator/index to point to the next location where they can start inserting data when the next call is made. QDataStream::operator>> probably starts to read from the same location.

QDataStream::resetStatus() does not change the position from where the object reads/writes. It merely resets the status to QDataStream::Ok to allow you to read from the stream after an error has occurred.

You can use two QDataStream objects -- one for writing to the QByteArray and the other for reading from the same QByteArray.

QByteArray byteArray;

QDataStream ds_w(&byteArray,QIODevice::WriteOnly);
QDataStream ds_r(&byteArray,QIODevice::ReadOnly);

ds_w << (qint32)20;

ds_w << bArray;

qint32 code;

ds_r >> code;
Megidd
  • 7,089
  • 6
  • 65
  • 142
R Sahu
  • 204,454
  • 14
  • 159
  • 270
  • 2
    ty R Sahu your codes worked too but i used ds.device()->reset(); – NetProjects Ir Jul 02 '16 at 18:52
  • note that you can use [`ds.device()->seek()`](https://doc.qt.io/qt-5/qiodevice.html#seek) to point to wherever you want (not just the beginning of the stream). – Mike Jul 03 '16 at 09:27