[Development] Weird offseting in QDataStream

André Somers andre at familiesomers.nl
Fri Nov 8 10:52:35 CET 2013


Yves Bailly schreef op 8-11-2013 10:42:
> Le 08/11/2013 10:29, Christian Ehrlicher a écrit :
>> Am 08.11.2013 10:15, schrieb Yves Bailly:
>>> Le 08/11/2013 10:05, Giuseppe D'Angelo a écrit :
>>>> On 8 November 2013 10:01, Yves Bailly <yves.bailly at sescoi.fr> wrote:
>>>>> As a float is 4 bytes, I would expect the second f.pos() to display "4"... but
>>>>> it displays "8", as if QDataStream had moved 8 bytes ahead instead of just 4.
>>>>> Needless to say, the read float is wrong...
>>>>>
>>>>> Am I missing something here, or is it a bug?
>>>> See QDataStream::setFloatingPointPrecision. The default is double.
>>> That's pretty strange... what if I want to read a float then a double? do I
>>> have to switch between the two each time?
>>> Does this means it's impossible to use code like this:
>>> float f;
>>> double d;
>>> datastream >> f >> d;
>>> ?
>> setFloatingPointPrecision does only change the way the floating point
>> numbers are stored in the data stream, not what you read from it.
> Sorry but this seems wrong to me... as soon as I used setFloatingPointPrecision(SinglePrecision),
> magically I can read the floats correctly. So setFloatingPointPrecision() *also* changes
> the way numbers are read.
Indeed.
>
>> Also I think you're wrong in what you want to do. You can't read data
>> from a file which was not stored with QDataStream before! See
>> http://qt-project.org/doc/qt-5.0/qtcore/qdatastream.html#details
> I can understand this for high-level Qt objects, but what about lower-level
> data? Does this mean I can't use QDataStream to read a file written by
> some other program, and/or can't use it to *write* a file which could be
> read back by some other program? Again, only using low-level data, ints,
> floats, and so on.
>
> If that is true, then it's a huge step backward in the ease of use provided by Qt
> in the realm of reading/writing binary files.
It is not a step backwards. It has been like this before Qt 5 too. I'm 
not sure when this change was introduced though, but probably early in 
Qt 4. Did you notice that you can set the version QDataStream is using 
to an earlier one? According to the docs, 
QDataStream::FloatingPointPrecision has no effect if the data stream 
version is below Qt_4_6, so perhaps that's when the change was introduced.
> Just to give a context, the goal here is to read and write binary
> STL (stereolithography) or PLY files. I have old Qt3 code which works like a
> charm, reading and writing files usable to and from other soft (e.g. Blender).
> Now moving that old code to Qt5 it seems to not work as expected, despite
> using only basic, low-level data types.
>
I think it would not be very hard to write your own wrapper for 
QDataStream that does the switching of type automatically, but it seems 
to me that even setting the data stream version to something before 
Qt_4_6 before starting to read should work as well.

André




More information about the Development mailing list