I am using QTableView and QStandardItemModel and I'm trying to colour a row with the font remaining black.
I am using my delegate class's paint method:
void Delegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
QBrush brush(Qt::red, Qt::SolidPattern);
painter->setBackground(brush);
}
This does not work at all and it makes the text within each cell transparent. What am I doing wrong here?
[EDIT]
I've used painter->fillRect(option.rect, brush);
as well but it makes the cell background and text the same colour.
Your Delegate
should inherit QStyledItemDelegate
.
Your paint event probably should look like this:
void Delegate::paint(QPainter *painter,
const QStyleOptionViewItem &option,
const QModelIndex &index) const
{
QStyleOptionViewItem op(option);
if (index.row() == 2) {
op.font.setBold(true);
op.palette.setColor(QPalette::Normal, QPalette::Background, Qt::black);
op.palette.setColor(QPalette::Normal, QPalette::Foreground, Qt::white);
}
QStyledItemDelegate::paint(painter, op, index);
}