Results 1 to 2 of 2

Thread: Changing the background color of a cell in a QTableView

  1. #1
    Join Date
    Jun 2010
    Location
    Cincinnati, Ohio, USA
    Posts
    92
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Windows

    Question Changing the background color of a cell in a QTableView

    I am trying to figure out how to change the background color of one cell, the changing of the actual text and changing the text alignment is also working correctly, but the background color isn't changing. Any thoughts?

    Qt Code:
    1. QVariant FavoriteSqlModel::data(const QModelIndex &index, int role) const
    2. {
    3. QVariant value = QSqlQueryModel::data(index, role);
    4.  
    5. if(index.column() == _statusIdx)
    6. {
    7. int status = value.toInt();
    8.  
    9. switch(role)
    10. {
    11. case Qt::DisplayRole:
    12. if(value.isValid())
    13. {
    14. switch(status)
    15. {
    16. case 0:
    17. return tr("");
    18. case 1:
    19. return tr("Uncopied");
    20. case 2:
    21. return tr("Modified");
    22. case 3:
    23. return tr("Copied");
    24. }
    25. }
    26. break;
    27. case Qt::TextAlignmentRole:
    28. return Qt::AlignHCenter | Qt::AlignVCenter;
    29. //case Qt::BackgroundColorRole:
    30. case Qt::BackgroundRole:
    31. switch(status)
    32. {
    33. case 0:
    34. case 1:
    35. // use the default
    36. break;
    37. case 2:
    38. return QColor(Qt::yellow);
    39. case 3:
    40. return QColor(Qt::darkGreen);
    41. }
    42. break;
    43. }
    44. }
    45.  
    46. return value;
    47. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jun 2010
    Location
    Cincinnati, Ohio, USA
    Posts
    92
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Changing the background color of a cell in a QTableView

    I figured it out, when the role was the BackgroundRole, I was switching on the background color value, not on the actual value (Qt:isplayRole). After the code gets into the Qt::BackgroundRole, I now do a int stateCode = QSqlQueryModel::data(index, Qt:isplayRole).toInt(); and switch off that, it works GREAT!

Similar Threads

  1. how to color a cell in a QTableView
    By JeanC in forum Qt Programming
    Replies: 13
    Last Post: 9th September 2015, 10:08
  2. QTableView and changing how a cell displays its contents
    By BitRogue in forum Qt Programming
    Replies: 2
    Last Post: 9th October 2009, 10:18
  3. Replies: 1
    Last Post: 23rd March 2009, 21:36
  4. prob with changing QLineEdit background color
    By Ahmad in forum Qt Programming
    Replies: 2
    Last Post: 30th April 2007, 12:44
  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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.