这个问题是这篇文章的进一步发展并且是不同的,虽然可能看起来与这个相似。
我正在尝试重新实现QHeaderView::paintSection
,以便从模型返回的背景得到尊重。我试着这样做
void Header::paintSection(QPainter * painter, const QRect & rect, int logicalIndex) const
{
QVariant bg = model()->headerData(logicalIndex, Qt::Horizontal, Qt::BackgroundRole);
// try before
if(bg.isValid()) // workaround for Qt bug https://bugreports.qt.io/browse/QTBUG-46216
painter->fillRect(rect, bg.value<QBrush>());
QHeaderView::paintSection(painter, rect, logicalIndex);
// try after
if(bg.isValid()) // workaround for Qt bug https://bugreports.qt.io/browse/QTBUG-46216
painter->fillRect(rect, bg.value<QBrush>());
}
但是,它没有用 - 如果我QHeaderView::paintSection
打电话,我用画家画的任何东西都不可见(我也尝试画一条对角线)。如果我删除QHeaderView::paintSection
呼叫,线路和背景将可见。fillRect
之前和之后拨打电话没有QHeaderView::paintSection
任何区别。
我想知道,是什么QHeaderView::paintSection
让我无法在它上面画一些东西。是否有办法在不重新实现所有内容的情况下克服它QHeaderView::paintSection
?
我需要做的就是为某个单元格添加某种阴影——我仍然希望单元格中的所有内容(文本、图标、渐变背景等)都像现在一样绘制......