Results 1 to 14 of 14

Thread: how to color a cell in a QTableView

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #3
    Join Date
    Dec 2007
    Location
    Groningen Netherlands
    Posts
    182
    Thanks
    16
    Thanked 2 Times in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: how to color a cell in a QTableView SOLVED

    Got it working thanks
    For those interested my code:

    in the .h file add:

    Qt Code:
    1. class Delegate : public QItemDelegate
    2. {
    3. Q_OBJECT
    4. public:
    5. Delegate(QWidget *parent = 0) : QItemDelegate(parent) {}
    6. void paint(QPainter *painter, const QStyleOptionViewItem &option,
    7. const QModelIndex &index) const;
    8. };
    To copy to clipboard, switch view to plain text mode 

    in the .cpp, in a constructor

    Qt Code:
    1. table->setItemDelegate(new Delegate);
    To copy to clipboard, switch view to plain text mode 

    and the event:

    Qt Code:
    1. //---------------------------------------------------
    2. void Delegate::paint(QPainter *painter, const QStyleOptionViewItem &option,
    3. const QModelIndex &index) const
    4. {
    5. QString val;
    6. if (qVariantCanConvert<QString>(index.data()))
    7. val = qVariantValue<QString>(index.data());
    8. if (val == "1")
    9. {
    10. painter->fillRect(option.rect, option.palette.highlight());
    11. }
    12. else
    13. QItemDelegate::paint(painter, option, index);
    14. }
    To copy to clipboard, switch view to plain text mode 

  2. The following 2 users say thank you to JeanC for this useful post:

    jiveaxe (14th January 2008), MarkoSan (13th January 2008)

Similar Threads

  1. how to remove the span set for a cell in QTableView
    By dandanch in forum Qt Programming
    Replies: 2
    Last Post: 21st October 2008, 11:22
  2. Word wrapping in a QTableWidget cell
    By jcooperddtd in forum Qt Programming
    Replies: 3
    Last Post: 1st May 2007, 03:57
  3. Replies: 2
    Last Post: 10th May 2006, 03:14
  4. Copying contents of QTableView cell to clipboard
    By Conel in forum Qt Programming
    Replies: 2
    Last Post: 18th April 2006, 15:50
  5. QTableView change color of current Cell
    By raphaelf in forum Qt Programming
    Replies: 4
    Last Post: 4th March 2006, 11:22

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.