PDA

View Full Version : Is it impossible to write 4 bytes for a float and 8 for double at QdataStream ?



tonnot
17th November 2011, 17:16
I need (I must) to write a lot of floats to a qdatastream and it is neccesary I use only 4 bytes.
setFloatingPointPrecision or writes 4 or 8 both for floats and doubles.
I thought that it was applicable to qreal, but I surprise it works on float a double type..

Is there any solution ? Thanks

MarekR22
17th November 2011, 20:58
Read documentation!!!
Documentation of QDataStream::setFloatingPointPrecision clearly says that this behavior writing doubles and floats depends on data steam version.
So you have to call setVersion with value QDataStream::Qt_4_5 or lower.
Note also that they recommend that you should read "Versioning" section.

ChrisW67
17th November 2011, 21:57
Is the float binary size an issue because you using a QDataStream to write a binary file to be interpreted by a non-Qt program? If so you probably should just avoid QDataStream and write directly to a QIODevice. If there is anything other than individual intrinsic data types in the QDataStream then chances are the other end will have fun interpreting it.

MarekR22
17th November 2011, 22:23
based on his other topics I would say it is good he is using this class. He plans to run his program with diffrent platforms so he have to handle endians and so on.

tonnot
18th November 2011, 07:08
The solution consist int having two functions, one for float and the other a typical template. For float QDataStream::setFloatingPointPrecision to 4 and 8 for the other. I dont know that QDataStream::setFloatingPointPrecision could be set at any moment, by mistake, I thought that the setting was make for all the process.
Thanks.

MarekR22
18th November 2011, 13:06
did you tried setVersion like I suggested in post #2?