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

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

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