Results 1 to 15 of 15

Thread: The precision range of double

  1. #1
    Join Date
    Jan 2008
    Location
    Bengaluru
    Posts
    144
    Thanks
    8
    Thanked 7 Times in 7 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Exclamation The precision range of double

    Hi,

    I store the list of QString , and i need to populate the QStandardItemModel. Here if i put the values as QString then the 'sort' on the column occurs by string comparision, as a result my decimal numbers arrange by string comaparision and the result of the QT::Ascending or QT:: Descending turns out to be wrong.
    Qt Code:
    1. Ex:
    2. value1 = 2.4567
    3. value2 = 1.34577
    4. value3 = 10.76546
    5. value4 = 11.45454
    6.  
    7. Result of Ascending order:
    8. 1.34577
    9. 10.76546
    10. 11.45454
    11. 2.4567, which is correct because of string compare, but numerically wrong.
    To copy to clipboard, switch view to plain text mode 
    Or if I put the values in Model by converting into double the range of the values are not coming properly. The 12 digit double is coming as 6 digit.

    Qt Code:
    1. Ex:
    2. QString value = 34.5634098790;
    3. double nValue = value.toDouble();
    4.  
    5. //nValue = 34.5634
    6.  
    7. Can you let me know how should i maintain the precision and to do ordering numerically
    8. in table model.
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: The precision range of double

    The precision is maintained. You only see the double truncated. If you want the full precision to be displayed, reimplement the delegate and force it to draw all the decimals by modifying the call to QString::number() that takes a precision (6 by default).
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Jan 2008
    Location
    Bengaluru
    Posts
    144
    Thanks
    8
    Thanked 7 Times in 7 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Re: The precision range of double

    Ok. Can you brief a little which one to reimplement which inturns call QString::number(). I am little confused.

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: The precision range of double

    The easiest way would be to derive from QStyledItemDelegate and reimplement QStyledItemDelegate::displayText().
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    Jan 2008
    Location
    Bengaluru
    Posts
    144
    Thanks
    8
    Thanked 7 Times in 7 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Re: The precision range of double

    Even i reimplement QStyledItemDelegate::displayText(...), the return type is again a string and the sorting happens based on string comparision.

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: The precision range of double

    What exactly did you do?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  7. #7
    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: The precision range of double

    Quote Originally Posted by nikhilqt View Post
    Can you let me know how should i maintain the precision and to do ordering numerically
    in table model.
    If you use QStandardItemModel::setSortRole() to set the role to Qt::EditRole and return a QVariant::Double in that role, and the QVariant::String form in the Qt::DisplayRole, does that achieve your sorting requirement?

  8. #8
    Join Date
    Jan 2008
    Location
    Bengaluru
    Posts
    144
    Thanks
    8
    Thanked 7 Times in 7 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Re: The precision range of double

    Quote Originally Posted by wysota View Post
    What exactly did you do?
    I just looked into the signature and the parameters of the method, as such it returns me the string only. Again if i click on the column header of the tableview it gives me string compare only. correct me if Iam wrong.

  9. #9
    Join Date
    Jan 2008
    Location
    Bengaluru
    Posts
    144
    Thanks
    8
    Thanked 7 Times in 7 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Re: The precision range of double

    Quote Originally Posted by ChrisW67 View Post
    If you use QStandardItemModel::setSortRole() to set the role to Qt::EditRole and return a QVariant:: Double in that role, and the QVariant::String form in the Qt:: DisplayRole, does that achieve your sorting requirement?
    I do not edit the contents of the table, I only click on the column header of the tableview, such that the sorting of Ascending and Descending happens.

  10. #10
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: The precision range of double

    Quote Originally Posted by nikhilqt View Post
    I just looked into the signature and the parameters of the method, as such it returns me the string only. Again if i click on the column header of the tableview it gives me string compare only. correct me if Iam wrong.
    So you didn't implement anything, just assumed it wouldn't work anyway?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  11. #11
    Join Date
    Jan 2008
    Location
    Bengaluru
    Posts
    144
    Thanks
    8
    Thanked 7 Times in 7 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Re: The precision range of double

    Quote Originally Posted by wysota View Post
    So you didn't implement anything, just assumed it wouldn't work anyway?
    I would rather put it this way, I started with the process and did not get how to change the precision.

    QStyledItemDelegate::displaytext(...) takes QVariant and the QLocale.

    After setting up this delegate to my model, this displaytext is called for showing the text on the view. But here i stuck up, as how to draw all the decimals.

  12. #12
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: The precision range of double

    Hmm... I even told you which method to use to get the desired output... I really don't understand what is so hard there to understand...

    Qt Code:
    1. QString myDelegate::displayText(const QVariant &val, const QLocale &locale) const {
    2. if(val.type()==QVariant::Double) return locale.toString(val.toDouble(), 'g', 12); // or QString::number(val.toDouble(), 'g', 12)
    3. return QStyledItemDelegate::displayText(val, locale);
    4. }
    To copy to clipboard, switch view to plain text mode 
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  13. #13
    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: The precision range of double

    Quote Originally Posted by nikhilqt View Post
    I do not edit the contents of the table, I only click on the column header of the tableview, such that the sorting of Ascending and Descending happens.
    This useful?
    Qt Code:
    1. #include <QtGui>
    2. #include <QDebug>
    3. #include <QStandardItemModel>
    4.  
    5. int main(int argc, char *argv[])
    6. {
    7.  
    8. QApplication app(argc, argv);
    9.  
    10. QStandardItemModel model(10, 2);
    11. model.setSortRole(Qt::EditRole);
    12. for (int row = 0; row < 10; ++row) {
    13. qreal value = row * 1.2345678;
    14. QString text;
    15. text.setNum(value, 'g', 10);
    16.  
    17. // Column with only a user readable DisplayRole
    18. QStandardItem *item1 = new QStandardItem(text);
    19. model.setItem(row, 0, item1);
    20.  
    21. // Column with sortable EditRole
    22. QStandardItem *item2 = new QStandardItem(text);
    23. item2->setData(value, Qt::EditRole);
    24. model.setItem(row, 1, item2);
    25. }
    26.  
    27. tv.setModel(&model);
    28. tv.setSortingEnabled(true);
    29. tv.horizontalHeader()->setSortIndicator(0, Qt::AscendingOrder);
    30. tv.horizontalHeader()->setSortIndicator(1, Qt::AscendingOrder);
    31. tv.show();
    32.  
    33. return app.exec();
    34. }
    35.  
    36. // vi: sw=4 ts=4 et
    To copy to clipboard, switch view to plain text mode 
    Column 1 sorts based on the string representation. Column 2 sorts numerically but displays in default format.

  14. #14
    Join Date
    Jan 2008
    Location
    Poland
    Posts
    687
    Thanks
    4
    Thanked 140 Times in 132 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: The precision range of double

    for me this is working as it should:
    Qt Code:
    1. #include "mainwindow.h"
    2. #include "ui_mainwindow.h"
    3.  
    4. #include <QStyledItemDelegate>
    5.  
    6. class MyDelegate : public QStyledItemDelegate
    7. {
    8. public:
    9. MyDelegate(QObject *parent = 0) : QStyledItemDelegate(parent) { }
    10. QString displayText(const QVariant &value, const QLocale &locale) const {
    11. if (value.type() == QVariant::Double) return locale.toString(value.toDouble(), 'g', 16);
    12. return QStyledItemDelegate::displayText(value, locale);
    13. }
    14. };
    15.  
    16. MainWindow::MainWindow(QWidget *parent)
    17. : QMainWindow(parent), ui(new Ui::MainWindow)
    18. {
    19. ui->setupUi(this);
    20. m_model = new QStandardItemModel(this);
    21. item->setData(3.8133400354, Qt::DisplayRole);
    22. m_model->appendRow(QList<QStandardItem *>() << item << new QStandardItem("text a"));
    23. item = new QStandardItem;
    24. item->setData(2.123003885, Qt::DisplayRole);
    25. m_model->appendRow(QList<QStandardItem *>() << item << new QStandardItem("text h"));
    26. item = new QStandardItem;
    27. item->setData(2.123003876, Qt::DisplayRole);
    28. m_model->appendRow(QList<QStandardItem *>() << item << new QStandardItem("text c"));
    29. item = new QStandardItem;
    30. item->setData(123.85934000053, Qt::DisplayRole);
    31. m_model->appendRow(QList<QStandardItem *>() << item << new QStandardItem("text a"));
    32. item = new QStandardItem;
    33. item->setData(13.85942403234, Qt::DisplayRole);
    34. m_model->appendRow(QList<QStandardItem *>() << item << new QStandardItem("text f"));
    35. item = new QStandardItem;
    36. item->setData(46.8593400354, Qt::DisplayRole);
    37. m_model->appendRow(QList<QStandardItem *>() << item << new QStandardItem("text g"));
    38. item = new QStandardItem;
    39. item->setData(3.2452334, Qt::DisplayRole);
    40. m_model->appendRow(QList<QStandardItem *>() << item << new QStandardItem("text d"));
    41. item = new QStandardItem;
    42. item->setData(8.4223400354, Qt::DisplayRole);
    43. m_model->appendRow(QList<QStandardItem *>() << item << new QStandardItem("text e"));
    44. ui->tableView->setModel(m_model);
    45. ui->tableView->setItemDelegate(new MyDelegate(this));
    46. }
    47.  
    48. MainWindow::~MainWindow()
    49. {
    50. delete ui;
    51. }
    To copy to clipboard, switch view to plain text mode 
    Attached Images Attached Images
    Last edited by faldzip; 8th July 2009 at 10:24. Reason: images added
    I would like to be a "Guru"

    Useful hints (try them before asking):
    1. Use Qt Assistant
    2. Search the forum

    If you haven't found solution yet then create new topic with smart question.

  15. #15
    Join Date
    Jan 2008
    Location
    Bengaluru
    Posts
    144
    Thanks
    8
    Thanked 7 Times in 7 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Re: The precision range of double

    It worked. It took little more time to get hang on it. sorry for that. I made wrong in overriding the displayText() method. My new signature of the method acted as overloaded method of that and the call for that displaytext() did not happen until i realize the mistake.

    Thanks for the patience.
    Last edited by nikhilqt; 8th July 2009 at 15:12.

Similar Threads

  1. Double Click Capturing
    By ToddAtWSU in forum Qt Programming
    Replies: 2
    Last Post: 8th January 2011, 14:12
  2. Spectrogram Plot Hints
    By shargath in forum Qwt
    Replies: 2
    Last Post: 25th February 2009, 11:11
  3. Replies: 1
    Last Post: 24th July 2008, 18:20
  4. Scaling
    By AD in forum Qt Programming
    Replies: 16
    Last Post: 11th July 2008, 10:55
  5. Double Buffering for plot graphs
    By Tavit in forum Qt Programming
    Replies: 0
    Last Post: 20th March 2008, 13:10

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.