Results 1 to 15 of 15

Thread: The precision range of double

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    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.

  2. #2
    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.

  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

    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
  •  
Qt is a trademark of The Qt Company.