PDA

View Full Version : Change focus/selection color for single cell in QTableView



jmalicke
31st October 2014, 19:23
I have a table that renders a cell background green or red depending on its value. Here's a picture:

10715

When I select/focus the table row, the green color is lost:

10716

I would like to keep a greenish color only for the single cell on focus/selection. Perhaps I would make it a slightly darker green color. How would I go about doing this?

Here is my code that renders the cell green or red:


QVariant ExternalDataSourcesTableModel::data(const QModelIndex & index, int32_t role) const
{
...

if (role == Qt::BackgroundRole)
{
if (index.column() == static_cast<int>(Column::STATUS)) {
if (externalDataSource.getStatus(reason) == "OK") {
return QColor(Qt::green);
}
else {
return QColor(Qt::red);
}
}
}

...
}

ChrisW67
31st October 2014, 21:45
You create a QStyledItemDelegate subclass that implements paint() the way you want and then attach it to the column of the view with QAbstractItemView::setItemDelegateForColumn()