Results 1 to 5 of 5

Thread: precision of float field in qtableview

  1. #1
    Join Date
    Nov 2006
    Posts
    16
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default precision of float field in qtableview

    I have a qtableview to display result of a sql query.I have problems with filelds that defined to have float data types.

    Table is defined in database as:
    table1(studentId int,
    studentGrade int,
    weight float)
    Here is how my table in database look like:
    Qt Code:
    1. studentId studentGrade weight
    2. 1 80 0.7
    3. 2 90 0.8
    To copy to clipboard, switch view to plain text mode 

    i fill qSqlQueryModel with query (select * from table1) and then set model to qtableview.
    But float fields' precision is lost.
    Here is how my QTabelView looks like:
    Qt Code:
    1. studentId studentGrade weight
    2. 1 80 0.69999999
    3. 2 90 0.7999999
    To copy to clipboard, switch view to plain text mode 

    anybody knows how to fix this?? i just want to see 0.7 and 0.8 in my qtableview.

    thanx in adnavce.

  2. #2
    Join Date
    Mar 2007
    Location
    Germany
    Posts
    229
    Thanks
    2
    Thanked 29 Times in 28 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: precision of float field in qtableview

    That is because floats and doubles cannot "exactly" store 0.7 or 0.8 (see Wikipedia: http://en.wikipedia.org/wiki/Floatin...uracy_problems).

    Do you use QString::setNum( double n, char format = 'g', int precision = 6 ) for setting the string?
    Play around with format an precision. precision rounds the number. So if you want 0.7 and not 0.70 or 0.69999 use precision = 1.

  3. #3
    Join Date
    Sep 2008
    Posts
    54
    Thanks
    3
    Thanked 10 Times in 5 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    2

    Default Re: precision of float field in qtableview

    Did you solve this problem? I am having the same problem and converting to string is nice but bad for sorting and alignment.

  4. #4
    Join Date
    Mar 2020
    Posts
    3
    Qt products
    Qt5
    Platforms
    MacOS X Windows

    Cool Re: precision of float field in qtableview

    Quote Originally Posted by mstegehu View Post
    Did you solve this problem? I am having the same problem and converting to string is nice but bad for sorting and alignment.
    Later years, someone can help me?

  5. #5
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    4,476
    Thanks
    252
    Thanked 709 Times in 699 Posts
    Qt products
    Qt5
    Platforms
    Windows Android

    Default Re: precision of float field in qtableview

    someone can help me?
    Help you do what? Be specific.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

Similar Threads

  1. boolean field checkbox QTableView
    By skuda in forum Qt Programming
    Replies: 4
    Last Post: 8th November 2010, 13:48
  2. Value changed in a QTableView field
    By nittalope in forum Newbie
    Replies: 4
    Last Post: 12th August 2009, 09:21
  3. Replies: 1
    Last Post: 10th June 2009, 21:59
  4. Some very weird compilation warnings
    By MarkoSan in forum Qt Programming
    Replies: 21
    Last Post: 23rd January 2008, 16:48
  5. How to know when field is edited within QTableView
    By dkite in forum Qt Programming
    Replies: 3
    Last Post: 1st January 2007, 18:51

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.