Results 1 to 9 of 9

Thread: How to set lower precision only for "view"

  1. #1
    Join Date
    Apr 2011
    Posts
    13
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default How to set lower precision only for "view"

    I am trying to implement a table widget which displays (real) numbers.
    I want to display data in the table with lower precision (say, number
    of digits after the decimal point is 2) without reducing the actual presicion of the referenced data.
    I thought that I can store the rounded and not-rounded data separately as
    DisplayRole and EditRole, respectively, but it turns out not true
    as far as looking at the sources of QStandardItemModel::setData() and
    QTableWidgetItem::setData().
    Qt Code:
    1. void QStandardItem::setData(const QVariant &value, int role)
    2. {
    3. role = (role == Qt::EditRole) ? Qt::DisplayRole : role;
    4. .......
    5. }
    To copy to clipboard, switch view to plain text mode 
    Should I prepare another instance of StandardItemModel or my own container to store unrounded data?

  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: How to set lower precision only for "view"

    You could just use a custom delegate that would reduce precision for Qt:isplayRole.
    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
    Apr 2011
    Posts
    13
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How to set lower precision only for "view"

    Hi, thank you very much for reply.

    I am using a custom delegate which uses QLineEdit as an editor.
    I set the rounded data to the editor as below, but it does not seem to work.
    The data are still shown as they are.
    I may be misunderstanding the role of delegate...
    Qt Code:
    1. LineEditDelegate::LineEditDelegate(QWidget *parent)
    2. : QStyledItemDelegate(parent), validator(new QDoubleValidator(this))
    3. {
    4. }
    5.  
    6. QWidget *LineEditDelegate::createEditor(QWidget *parent,
    7. const QStyleOptionViewItem &option,
    8. const QModelIndex &index) const
    9. {
    10. QLineEdit *editor = new QLineEdit(parent);
    11. editor->setValidator(validator);
    12. return editor;
    13. }
    14.  
    15. void LineEditDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
    16. {
    17. QLineEdit *e = qobject_cast<QLineEdit *>(editor);
    18. e->setText(QString::number(index.data(Qt::EditRole).toDouble(), 'f', 2));
    19. }
    20.  
    21. void LineEditDelegate::setModelData(QWidget *editor, QAbstractItemModel *model,
    22. const QModelIndex &index) const
    23. {
    24. QLineEdit *e = qobject_cast<QLineEdit *>(editor);
    25. model->setData(index, e->text(), Qt::EditRole);
    26. }
    To copy to clipboard, switch view to plain text mode 
    You could just use a custom delegate that would reduce precision for Qt:isplayRole.
    But as far as DisplayRole and EditRole are not actually distinguished,
    won't it overwrite the original with the rounded data?

  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: How to set lower precision only for "view"

    I don't see any benefits of editing an approximated value. I could see the benefit of displaying an approximated value in regular display and editing a precise value in the editor.
    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
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: How to set lower precision only for "view"

    You could derive a class from QStandardItemModel and override setData() and data() so that the two roles are distinguished. Unless you want more customization, the only roles you need to override are EditRole and DisplayRole - you can simply return QVariant() for everything else and let the base class handle it. You might even be able to get away with simply overriding the data() method. Since you know that setData() always puts the value into the DisplayRole, you use setData() with full precision but return the reduced precision in the data( Qt::DisplayRole )

  6. #6
    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: How to set lower precision only for "view"

    Displaying a certain number of decimal places for doubles in the table widget should be as simple as using a delegate like:
    Qt Code:
    1. class Delegate: public QStyledItemDelegate {
    2. Q_OBJECT
    3. public:
    4. Delegate(QObject *p = 0): QStyledItemDelegate(p) { }
    5.  
    6. QString displayText(const QVariant &value, const QLocale &locale) const
    7. {
    8. if (value.type() == QVariant::Double) {
    9. return locale.toString(value.toDouble(), 'f', 2);
    10. }
    11. else
    12. return QStyledItemDelegate::displayText(value, locale);
    13. }
    14. };
    To copy to clipboard, switch view to plain text mode 
    This is quite distinct from anything to do with what is displayed in an editor for that value.

  7. #7
    Join Date
    Apr 2011
    Posts
    13
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How to set lower precision only for "view"

    Quote Originally Posted by ChrisW67 View Post
    Displaying a certain number of decimal places for doubles in the table widget should be as simple as using a delegate like:
    Hi, thank you for reply.

    This is exactly what I wanted!
    I've never noticed the presence of virtual function: QString QStyledItemDelegate::displayText(const QVariant &value, const QLocale &locale) const.

    But it is a pity that I cannot access the model index from this function.
    I want to set different precision for each column...
    Last edited by lain; 26th March 2012 at 01:34.

  8. #8
    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: How to set lower precision only for "view"

    Apply a different delegate to each column QAbstractItemView::setItemDelegateForColumn(). If all that varies is the number of decimal places then you can use the same class with a parameter (passed in at construction of the delegate).

  9. The following user says thank you to ChrisW67 for this useful post:

    lain (26th March 2012)

  10. #9
    Join Date
    Apr 2011
    Posts
    13
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How to set lower precision only for "view"

    Quote Originally Posted by ChrisW67 View Post
    Apply a different delegate to each column QAbstractItemView::setItemDelegateForColumn(). If all that varies is the number of decimal places then you can use the same class with a parameter (passed in at construction of the delegate).
    It did the trick.
    Thank you very much for the prompt and helpful reply!!

Similar Threads

  1. Replies: 2
    Last Post: 10th November 2010, 07:25
  2. Replies: 1
    Last Post: 7th April 2010, 21:46
  3. Replies: 3
    Last Post: 8th July 2008, 19:37
  4. Translation QFileDialog standart buttons ("Open"/"Save"/"Cancel")
    By victor.yacovlev in forum Qt Programming
    Replies: 4
    Last Post: 24th January 2008, 19:05
  5. QFile Problem~ "Unknow error" in "open(QIODevice::ReadWrite)"
    By fengtian.we in forum Qt Programming
    Replies: 3
    Last Post: 23rd May 2007, 15:58

Tags for this Thread

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.