Results 1 to 10 of 10

Thread: QTableView and display of doubles

  1. #1
    Join Date
    Jun 2006
    Posts
    64
    Thanks
    10
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default QTableView and display of doubles

    G'Day,

    I have a QSqlTableModel being displayed in a QTableView and the data comes from an Sqlite database.

    The data to be displayed is of type double (valid data is 0.0 to 15.0) and it all displays correctly except when the data ends in ".0". So "13.2" displays as "13.2", "14.5" displays as "14.5" however "15.0" displays as "15", "12.0" displays as "12". When editing the data it displays correctly as I have created my own DoubleSpinBox delegate and set the maximum, minimum, step and decimals to my needs. Editing works, just the displaying prior to editing that is not how I would like it.

    I managed to work out how to fix the editing side of it but I am stumped with the display side. I am still getting my head around the model concept.

    Any suggestions on where to look, read, or how to go about solving this would be appreciated. Its not a show stopper, I would just like it to look consistent.

    Thank you, B1.

  2. #2
    Join Date
    Aug 2010
    Posts
    107
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QTableView and display of doubles

    I am not sure how you are setting the data but you could try something like this:
    Qt Code:
    1. model->setData(model->index(row,column),QString::number(yourNumber, 'f', 3));
    To copy to clipboard, switch view to plain text mode 

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

    Default Re: QTableView and display of doubles

    The model is returning a QVariant double and the default delegate is formatting that with QLocale::toString() using the 'g' format. I would suggest you tackle this with a QStyledItemDelegate attached to the view or just to the double column(s). It need not do much except implement displayText() for double columns.

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

    b1 (16th November 2011)

  5. #4
    Join Date
    Jun 2006
    Posts
    64
    Thanks
    10
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTableView and display of doubles

    Thank you poporacer and ChrisW67,

    I will try both methods tonight when I get home from work.

    Thanks again, B1.

  6. #5
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    4,137
    Thanks
    235
    Thanked 660 Times in 650 Posts
    Qt products
    Qt5
    Platforms
    Windows Android

    Default Re: QTableView and display of doubles

    Quote Originally Posted by poporacer View Post
    I am not sure how you are setting the data but you could try something like this:
    Qt Code:
    1. model->setData(model->index(row,column),QString::number(yourNumber, 'f', 3));
    To copy to clipboard, switch view to plain text mode 
    If he does this, then the contents of the model in that cell will be a QString and not a double. What "b1" wants to do is to format the display of a double in the table, not to change the underlying model itself. In this case, Chris's method is the correct approach.

  7. The following user says thank you to d_stranz for this useful post:

    b1 (16th November 2011)

  8. #6
    Join Date
    Jun 2006
    Posts
    64
    Thanks
    10
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTableView and display of doubles

    Thank you all for the replies. ChrisW67 gave me the solution needed. I had already reimplemented a spinbox for editing so all I had to do was reimplement the drawText function for the displayed text.

    Qt Code:
    1. void SpinBoxDelegate::paint(QPainter * painter, const QStyleOptionViewItem & option, const QModelIndex & index) const
    2. {
    3. double value = index.model()->data(index, Qt::EditRole).toDouble();
    4. QString str;
    5. painter->drawText(option.rect, str.setNum(value, 'f', 1) );
    6. }
    To copy to clipboard, switch view to plain text mode 

    My problem now is when the row is selected the background goes dark blue, as expected, however the text remains black rather than going white, making it impossible to read. Any suggestions to help solve this would be appreciated.

    B1.
    Last edited by b1; 16th November 2011 at 21:01. Reason: Correct typo in code sample

  9. #7
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,552
    Thanks
    13
    Thanked 1,579 Times in 1,508 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QTableView and display of doubles

    If you want custom painting then you have to do the work to honour selections and the like and adjust the colours accordingly. You use the data in the QStyleOptionViewItem to achieve that, e.g:
    Qt Code:
    1. // something like this
    2. painter->save();
    3. if (option.state & QStyle::State_Selected) {
    4. painter->fillRect(option.rect, option.palette.highlight());
    5. painter->setPen(...); // use option.palette.highlightedText()
    6. painter->drawText(...);
    7. }
    8. else {
    9. painter->fillRect(option.rect, option.palette.base());
    10. painter->setPen(...); // use option.palette.text()
    11. painter->drawText(...);
    12. }
    13. painter->restore();
    To copy to clipboard, switch view to plain text mode 
    That's why I suggested just overriding QStyledItemDelegate::displayText() to change just the text that the default delegate painting then renders.

  10. #8
    Join Date
    Jun 2006
    Posts
    64
    Thanks
    10
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTableView and display of doubles

    Thanks ChrisW67,

    I thought I would try the overriding idea and I have failed to get it to work.

    Qt Code:
    1. QString SpinBoxDelegate::displayText(const QVariant &value, const QLocale &locale) const
    2. {
    3. if (value.userType() == QVariant::Double )
    4. {
    5. return locale.toString(value.toDouble(), 'f', 1);
    6. }
    7. else
    8. {
    9. return QStyledItemDelegate::displayText( value, locale);
    10.  
    11. }
    12. };
    To copy to clipboard, switch view to plain text mode 

    This doesn't compile with this error message:

    error: cannot call member function 'virtual QString QStyledItemDelegate::displayText(const QVariant&, const QLocale&) const' without object
    I didn't think an object was needed in this case...
    All the examples I have found seem to do it this way so any suggestions on where I have gone wrong would be appreciated?

    Thanks, B1.

  11. #9
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    4,137
    Thanks
    235
    Thanked 660 Times in 650 Posts
    Qt products
    Qt5
    Platforms
    Windows Android

    Default Re: QTableView and display of doubles

    Is your SpinBoxDelegate class derived from QStyledItemDelegate? If it is not, then you can't call a member function for a class that is unrelated to yours. That's what the compiler is trying to tell you: since your class is not derived from QStyledItemDelegate, "this" is not a QStyledItemDelegate, so in order to call one of the QStyledItemDelegate methods, you need an instance of that kind of object.

    The solution is simple:

    Qt Code:
    1. class SpinBoxDelegate : public QStyledItemDelegate
    2. {
    3. // ...
    4. };
    To copy to clipboard, switch view to plain text mode 

    Then your displayText() method will compile and work as you expect.

  12. The following user says thank you to d_stranz for this useful post:

    b1 (18th November 2011)

  13. #10
    Join Date
    Jun 2006
    Posts
    64
    Thanks
    10
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTableView and display of doubles

    Thank you d_stranz,

    You are correct, it was simple and it worked. I couldn't see the wood for the trees!!!

    Thank you to all who answered,

    B1.

Similar Threads

  1. Replies: 5
    Last Post: 25th May 2011, 10:10
  2. How to display periodically updated data in QTableView
    By nickla in forum Qt Programming
    Replies: 12
    Last Post: 15th March 2011, 21:33
  3. How do I display a picture on a QTableView cell?
    By danielperaza in forum Qt Programming
    Replies: 16
    Last Post: 9th April 2010, 22:04
  4. Replies: 2
    Last Post: 7th June 2009, 10:47
  5. QTableView does not display time string correctly
    By ad5xj in forum Qt Programming
    Replies: 1
    Last Post: 5th August 2007, 20:35

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.