code_junkie

In a QTableWidget, changing the text color of the selected row

Nov 12th, 2011
290
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.20 KB | None | 0 0
  1. class MyItemDelegate: public QItemDelegate
  2. {
  3. public:
  4. MyItemDelegate(QObject* pParent = 0) : QItemDelegate(pParent)
  5. {
  6. }
  7.  
  8. void paint(QPainter* pPainter, const QStyleOptionViewItem& rOption, const QModelIndex& rIndex) const
  9. {
  10. QStyleOptionViewItem ViewOption(rOption);
  11.  
  12. QColor ItemForegroundColor = rIndex.data(Qt::ForegroundRole).value<QColor>();
  13. if (ItemForegroundColor.isValid())
  14. {
  15. if (ItemForegroundColor != rOption.palette.color(QPalette::WindowText))
  16. {
  17. ViewOption.palette.setColor(QPalette::HighlightedText, ItemForegroundColor);
  18. }
  19. }
  20. QItemDelegate::paint(pPainter, ViewOption, rIndex);
  21. }
  22. };
  23.  
  24. QTableWidget* pTable = new QTableWidget(...);
  25. pTable->setItemDelegate(new MyItemDelegate(this));
  26.  
  27. QVariant MySortFilterProxyModel::data(const QModelIndex & index, int role = Qt::DisplayRole) {
  28. // assuming error state and modelindex row match
  29. if (role==Qt::BackgroundRole)
  30. return Qt::red;
  31. }
  32.  
  33. if (noError)
  34. {
  35. pTable->setStyleSheet("QTableView {selection-background-color: #000000; selection-color: #FFFFFF;}");
  36. }
  37. else
  38. {
  39. pTable->setStyleSheet("QTableView {selection-background-color: #FF0000; selection-color: #0000FF;}");
  40. }
Add Comment
Please, Sign In to add comment