Results 1 to 10 of 10

Thread: QTableView and display of doubles

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: 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.

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

  3. #3
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,330
    Thanks
    317
    Thanked 871 Times in 858 Posts
    Qt products
    Qt5
    Platforms
    Windows

    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.

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

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