4

I am using commmon css for our product which has templates for QTableView as below

QTableView::item
{
    padding-left:10px;
    height:40px;
    width:80px;
    color: #5a5a5a;
    border-bottom :1px solid #f0f0f0;
}

In one case, I want to change the text color of QTableView as red. I am doing it by following code in data function of QAbstractTableModel

if (role == Qt::TextColorRole || role == Qt::ForegroundRole)
    {
        QColor color(Qt::red);
        QBrush brush (color);
        brush.setStyle(Qt::SolidPattern);
        return brush;
    }

But, color of the text is not changing, it is taking color from the CSS. Any help would be appreciated.

1 Answers1

1

I did the following for textcolor changes in what is essentially QSqlTableModel, sub classed to CustomSqlTableModel to allow for overriding setData() and submit, and also to add a custom refresh function. The following should work just the same for QAbstractItemModel instances:

QVariant ExampleSqlTableModel::data(const QModelIndex &idx, int role) const
{
  if (role == Qt::ForegroundRole)
  {
    QColor color;
    // Only the display names need to be coloured.
    if(idx.column() == 1)
    {
      int value = CustomSqlTableModel::data(1, Qt::DisplayRole).toInt();
      switch(value)
      {
        case 0:         color = QColor(Qt::yellow); break; 
        case 1:         // same as 2
        case 2:         color = QColor(Qt::blue);   break; 
        case 3:         color = QColor(Qt::green);  break; 
        case 4:         // same as 5
        case 5:         color = QColor(Qt::red);    break; 
        default:        color = QColor(Qt::black);  break; 
      }
    }
    return QVariant(color);
  }
  return CustomSqlTableModel::data(idx,role);
}

I would also advise being a bit more specific with your widgets. Subclass all the QTableView instances that you do want your stylesheet to affect, and don't apply a general stylesheet to QTableView if it is going to affect the text color.

That way, if all other QTableViews have been sub classed to be myAwesomeTableView, then you could replace your stylesheet content to be:

myAwesomeTableView::item
{
    padding-left:10px;
    height:40px;
    width:80px;
    color: #5a5a5a;
    border-bottom :1px solid #f0f0f0;
}

If you always want the other QTableView to have red text, rather define a second subclass, and handle it via stylesheet instead of implementing it in code like I did. I needed different rows to have different text colors on a single column based on the value in that column. To do it via stylesheet, make another QTableView subclass, for argument, myOtherAwesomeTableView, and implement stylesheet as follows:

myOtherAwesomeTableView::item
{
    padding-left:10px;
    height:40px;
    width:80px;
    color: #ff0000;
    border-bottom :1px solid #f0f0f0;
}

Let me know if this helps you...

AAEM
  • 1,837
  • 2
  • 18
  • 26
Louis Parkin
  • 800
  • 7
  • 15
  • Hitsugaya198: Thanks for your reply. displaying text color in red is based on conditions, so, I can't make the changes in style sheet. I want my table to look same as my all other table (which has a css applied to it) in normal case, but, in some condition I want to show text in red color. I tried returning red color as QVariant from data function. but, that doesn't seem to work. Even I tried changing the object name for table and apply a different stylesheet which does not have color in it, but in that case, color gets inherited from parent table. Is there any other way to override it? – manoj kumar Feb 15 '16 at 12:14
  • I think, first split it from the stylesheet (or temporarily remove the color element from the stylesheet). If that doesn't work, try calling the following two lines from within a subclassed QTableView: style()->unpolish(this); style()->polish( this ); I do that for textboxes, spinboxes and tableviews that require runtime colour changes. To be clear, subclass this one tableview, and make the colour change a function of the view. when you the view is refreshed (when the AbstractTableView fires dataChanged or modelReset signals, call the above lines. – Louis Parkin Feb 15 '16 at 12:20
  • returning color as Qvariant works when color element is not there in stylesheet, But, my requirement is to use stylesheet and override the color in some condition. – manoj kumar Feb 15 '16 at 12:24
  • I have another idea, but it's too much text to fit in a comment. Try this collabedit link. [CollabEdit](http://collabedit.com/cqbuh) Will chat to you there. – Louis Parkin Feb 15 '16 at 12:35