Trolltech Home | Qt-interest Home | Recent Threads | All Threads | Author | Date
All threads index page 4

Qt-interest Archive, April 2007
QDataStream reading oddness


Message 1 in thread

Hi List,


I notice that the format for reading / writing a bool isn't listed in
the "Format of the QDataStream Operators" page in the docs - I assume
that it's represented as a quint8 or something similar..

Anyway, I have the following odd behaivour:

<code>
QByteArray input;
QDataStream inputReader(input);

bool reply;

input.clear();
input = sock.read();
inputReader.device()->seek(0);

qDebug("Recieved: %d", input.at(0));
</code>


At this point, input[0] contains a 1. So imagine my surprise when:

<code>
inputReader >> reply;
</code>

Leaves reply set to false!

I'm not sure what the behaivour is regarding QDataStreams and their
underlying QIODevices when the devices are modified outside of the
QDataStream... But I think the code above should work. 

Or am I missing something here?

-- 
 [ signature omitted ] 

Message 2 in thread

Hello List,


I've figured out the problem..

Basically, "QDataStream::device()->seek(0)" doesn't work correctly in
all situations.

Here's the minimal code example:

<code>
	// first, create a byte array that contains soem serialised
data:
	QString str1("This is the first string.. blah blah blah!");
	QByteArray data;
	QDataStream dsWriter(&data, QIODevice::WriteOnly);

	// write data:
	dsWriter << str1;

	// crate second data array:
	QByteArray data2;
	QDataStream dsReader(data2);

	// copy data into second array:
	data2 = data;
	// tell the data stream to seek to the beginning of the device:
	dsReader.device()->seek(0);

	// extract string from second array:
	QString strOut;
	dsReader >> strOut;
	
	// ERROR: this will print "", instead of the string.
	qDebug() << strOut;
</code>

Is this a bug? I've searched the QT docs, and I can't see any reference
to this situation. At best it's an omission from the docs, surely?


Cheers,


-- 
 [ signature omitted ] 

Message 3 in thread

Hi,

> Basically, "QDataStream::device()->seek(0)" doesn't work correctly in
> all situations.

I'm not certain this is related to seeking in the QDataStream device.

> Here's the minimal code example:
> [...]

This works as you expect it to work:
     QByteArray data;
     QDataStream dsWriter(&data, QIODevice::WriteOnly);
     dsWriter << QString("0123456789");
     QByteArray data2;
//--------------
     data2 = data;
     QDataStream dsReader(data2);
//--------------
     QString strOut;
     dsReader >> strOut;
     qDebug() << strOut;

This doesn't:
     QByteArray data;
     QDataStream dsWriter(&data, QIODevice::WriteOnly);
     dsWriter << QString("0123456789");
     QByteArray data2;
//--------------
     QDataStream dsReader(data2);
     data2 = data;
//--------------
     QString strOut;
     dsReader >> strOut;
     qDebug() << strOut;

My guess is that QDataStream buffers the contents of the device as soon as it 
is initialized. Even if the device is modified afterwards, it's too late, the 
data has been read.

--
 [ signature omitted ]