Results 1 to 14 of 14

Thread: how to color a cell in a QTableView

Hybrid View

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

  3. #2
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: how to color a cell in a QTableView

    I was wondering if QTableWidgetItem::setBackground could be of some use in this case ??

    one can get the tablewidgetitem(the cell) and set the bacground to it.

  4. #3
    Join Date
    Feb 2007
    Posts
    73
    Thanks
    11
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: how to color a cell in a QTableView

    Using the delegate works fine if I want to color the text. But I need to color the background of specific cells in the table. I have the following in my delegate:
    Qt Code:
    1. void MyDelegate::paint(
    2. QPainter *painter,
    3. QStyleOptionViewItem const & option,
    4. QModelIndex const & index) const
    5. {
    6. QStyleOptionViewItem opt(option);
    7.  
    8. if (index.column() == _colorColumn)
    9. {
    10. opt.palette.setColor(
    11. QPalette::Base,
    12. QColor(255, 255, 204));
    13.  
    14. opt.palette.setColor(
    15. QPalette::Window,
    16. QColor(255, 255, 204));
    17. }
    18.  
    19. return QItemDelegate::paint(painter, opt, index);
    20. }
    To copy to clipboard, switch view to plain text mode 

    Where _colorColumn is the column that I want the background colored. It's a member variable that gets initialized in the delegate's ctor.

    And it's not working. Any ideas what I can do next?

  5. #4
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: how to color a cell in a QTableView

    Wouldn't it be easier to define a model and use Qt::BackgroundColorRole and Qt::TextColorRole in the 'data' method with the default delegate?

    Eg.

    Qt Code:
    1. if (role == Qt::TextColorRole) {
    2. if (index.column() == _colorColumn) {
    3. return Qt::red;
    4. }
    5. }
    6. if (role == Qt::BackgroundColorRole) {
    7. if (index.column() == _colorColumn) {
    8. return Qt::blue;
    9. }
    10. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by squidge; 30th October 2009 at 23:06.

  6. The following user says thank you to squidge for this useful post:

    smacchia (2nd November 2009)

  7. #5
    Join Date
    Jul 2009
    Posts
    36
    Thanks
    6
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Re: how to color a cell in a QTableView

    Hi,

    You can do it simply by using setForeground() for text color and setBackground() for cell background color.

    By using

    tbl_item = new QTableWidgetItem (QTableWidgetItem (QString (SList.at(0))));
    tbl_item->setBackground(QBrush(QColor(Qt::blue)));



    I think it is useful for You.

  8. The following user says thank you to sosanjay for this useful post:

    smacchia (2nd November 2009)

  9. #6
    Join Date
    Feb 2007
    Posts
    73
    Thanks
    11
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Thumbs down Re: how to color a cell in a QTableView

    This won't work for me because I'm not use QTableWidget. I'm using QTableView with my own table model.

    As I said, changing the color of the text using the QPalette::WindowText works. It's changing the background of the table that isn't taking effect. Neither QPalette::Base or QPalette:Window work.

  10. #7
    Join Date
    Feb 2007
    Posts
    73
    Thanks
    11
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: how to color a cell in a QTableView

    Quote Originally Posted by fatjuicymole View Post
    Wouldn't it be easier to define a model and use Qt::BackgroundColorRole and Qt::TextColorRole in the 'data' method with the default delegate?

    Eg.

    Qt Code:
    1. if (role == Qt::TextColorRole) {
    2. if (index.column() == _colorColumn) {
    3. return Qt::red;
    4. }
    5. }
    6. if (role == Qt::BackgroundColorRole) {
    7. if (index.column() == _colorColumn) {
    8. return Qt::blue;
    9. }
    10. }
    To copy to clipboard, switch view to plain text mode 
    I tried this and it also didn't work

  11. #8
    Join Date
    Feb 2007
    Posts
    73
    Thanks
    11
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Thumbs up Re: how to color a cell in a QTableView

    Well believe it or not I got it to work using the role and the ::data method. However, I had to respond to a role of Qt::BackgroundRole, not Qt::BackgroundColorRole (I figured this out by looking at the Qt source for qitemdelegate.cpp). This is what worked:
    Qt Code:
    1. if (role == Qt::BackgroundRole && index.column() == _colorColumn)
    2. return QColor(255,255,204);
    To copy to clipboard, switch view to plain text mode 

  12. #9
    Join Date
    Jan 2010
    Location
    Pennsylvania - USA
    Posts
    1
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: how to color a cell in a QTableView

    I used the QModelIndex's model to color the cell using the setData method that was mentioned above, but as part of slot rather than a delegate. This will highlight whatever cell the user clicks on.

    Qt Code:
    1. void MyParentWidget::init()
    2. {
    3. connect(((QAbstractItemView*)this->table), SIGNAL(pressed(const QModelIndex&)),
    4. this, SLOT(highlightCell(const QModelIndex&)));
    5. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. void MyParentWidget::highlightCell(const QModelIndex &cellIndex)
    2. {
    3. for(int i=0; i<cellIndex.model()->columnCount(); i++)
    4. {
    5. for(int j=0; j<cellIndex.model()->rowCount(); j++)
    6. {
    7. if(i == cellIndex.column() && j == cellIndex.row())
    8. {
    9. ((QStandardItemModel*)cellIndex.model())->item(cellIndex.row(), i)->setData(QBrush(Qt::yellow),
    10. Qt::BackgroundRole);
    11. }
    12. else
    13. {
    14. ((QStandardItemModel*)cellIndex.model())->item(cellIndex.row(), i)->setData(QBrush(Qt::white),
    15. Qt::BackgroundRole);
    16. }
    17. }
    18. }
    19. }
    To copy to clipboard, switch view to plain text mode 

    You can modify & extend this behavior to meet your needs.

  13. #10
    Join Date
    Jan 2010
    Posts
    5
    Thanked 7 Times in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: how to color a cell in a QTableView

    just overide the data function in your model, it looks like this
    Qt Code:
    1. QVariant MyTestModel::data(const QModelIndex &index, int role) const
    2. {
    3. switch(role)
    4. {
    5. case Qt::DisplayRole:
    6. return QVariant(QString(tr("%1")).arg((index.column() + 1) * 1000 + index.row() + 1));
    7. case Qt::BackgroundRole:
    8. switch(index.column() % 3)
    9. {
    10. case 0:
    11. return QVariant(QColor(Qt::red));
    12. case 1:
    13. return QVariant(QColor(Qt::green));
    14. case 2:
    15. return QVariant(QColor(Qt::blue));
    16. default://only to disable warning
    17. return QVariant(QColor(Qt::white));
    18. }
    19.  
    20. break;
    21. default:
    22. return QVariant();
    23. }
    24. }
    To copy to clipboard, switch view to plain text mode 

    the BackgroundRole for the item's background color
    the ForegroundRole for the text color

  14. #11
    Join Date
    May 2010
    Location
    Rzeszow/Poland
    Posts
    17
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: how to color a cell in a QTableView

    Reassuming:

    How to set font-color to red if value in specified cell is for example not in specified range (int)?

  15. #12
    Join Date
    May 2011
    Posts
    4
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: how to color a cell in a QTableView

    Just a sidenote - from a usability point of view, in a majority of cases you probably don't want to paint the entire background.

    Screenshot: 2015-09-09 13_37_16-voima - Forceproof Analyst.png

    In many cases, using a Qt:: DecorationRole is more aesthetic and less obtrusive than filling the entire cell background. The text is still readable; you don't have to think about the contrast between changed foreground and background colours, since DecorationRole only paints a small rectangle of the color you selected, inside the cell.
    Qt Code:
    1. QVariant MyModel::data(const QModelIndex &index, int role) const
    2. {
    3. int column=index.column();
    4. if((role==Qt::DecorationRole)){
    5. if(column==firstVisibleColumnIndex){
    6. // figure out correct color for current row here
    7. return QColor(100,255,255);
    8. }
    9. }
    10. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by pilpi; 9th September 2015 at 11:41.

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.