【发布时间】:2015-06-15 14:05:05
【问题描述】:
这个问题是this post 的进一步发展,虽然看起来与this one 相似,但有所不同。
我正在尝试重新实现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 调用,线条和背景将可见。
在QHeaderView::paintSection 之前和之后进行fillRect 调用没有任何区别。
我想知道,QHeaderView::paintSection 做了什么让我无法在它上面画东西。
以及是否有一种方法可以在不重新实现 QHeaderView::paintSection 所做的一切的情况下克服它?
我需要做的就是为某个单元格添加某种阴影 - 我仍然希望单元格中的所有内容(文本、图标、渐变背景等)都像现在一样绘制...
【问题讨论】:
-
bg.value<QBrush>()会返回什么?它是有效的QBrush吗? -
模型将返回一个 QBrush 或一个空的 QVariant(如果不需要自定义背景)。 bg.isValid 负责后者。所以,是的 - bg.value
() 返回有效的画笔。如果我评论 QHeaderView::paintSection 调用,我可以看到矩形被正确填充 -
QHeaderView::paintSection正在您的矩形顶部绘制...它可能会覆盖它...尝试更改调用顺序 -
我之前和之后都试过了,结果一样 - 什么都没有显示
-
我之前和之后都试过了 - 更新了代码示例 - 都不行
标签: c++ qt qpainter qheaderview