我正在使用 QTableView 和 QStandardItemModel,我正在尝试为一行着色,字体保持黑色。
我正在使用我的委托(delegate)类的 paint 方法:
void Delegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
QBrush brush(Qt::red, Qt::SolidPattern);
painter->setBackground(brush);
}
这根本不起作用,它使每个单元格中的文本透明。我在这里做错了什么?
[编辑]
我也使用了 painter->fillRect(option.rect, brush);
但它使单元格背景和文本颜色相同。
最佳答案
您的Delegate
应该继承QStyledItemDelegate
。
您的绘画事件可能应该如下所示:
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);
}
关于c++ - QTableView,设置单元格的字体和背景颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35575564/