Results 1 to 6 of 6

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

  1. #1
    Join Date
    Sep 2010
    Posts
    654
    Thanks
    56
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Is it impossible to write 4 bytes for a float and 8 for double at QdataStream ?

    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

  2. #2
    Join Date
    Nov 2010
    Posts
    315
    Thanked 53 Times in 51 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Is it impossible to write 4 bytes for a float and 8 for double at QdataStream ?

    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.

  3. #3
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Is it impossible to write 4 bytes for a float and 8 for double at QdataStream ?

    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.

  4. #4
    Join Date
    Nov 2010
    Posts
    315
    Thanked 53 Times in 51 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Is it impossible to write 4 bytes for a float and 8 for double at QdataStream ?

    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.

  5. #5
    Join Date
    Sep 2010
    Posts
    654
    Thanks
    56
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Is it impossible to write 4 bytes for a float and 8 for double at QdataStream ?

    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.

  6. #6
    Join Date
    Nov 2010
    Posts
    315
    Thanked 53 Times in 51 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Is it impossible to write 4 bytes for a float and 8 for double at QdataStream ?

    did you tried setVersion like I suggested in post #2?

Similar Threads

  1. Replies: 2
    Last Post: 2nd November 2010, 06:15
  2. float vs double
    By Royceybaby in forum General Programming
    Replies: 4
    Last Post: 27th January 2010, 22:55
  3. WriteFile in QT... Too many bytes to write?
    By AlphaWolfXV in forum Qt Programming
    Replies: 1
    Last Post: 16th September 2009, 02:22
  4. QDataStream extra bytes in output?
    By grellsworth in forum Qt Programming
    Replies: 6
    Last Post: 15th November 2007, 14:41
  5. socket read/write bytes
    By nowire75 in forum Newbie
    Replies: 3
    Last Post: 5th July 2007, 00:12

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.