QDataStream and QByteArray as a buffer
-
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?
-
Stream's status becomes QDataStream::ReadPastEnd so i think i just misunderstood logic of streaming.
-
Indeed. You have to seek() back to the beginning of the buffer.
-
Do you mean QDataStream::device().seek()?
-
Yes (remember that QByteArray isn't a QIODevice, thus QDataStream creates a QBuffer internally).