What does QHeaderView :: paintSection do so that anything I do to the artist before or after is ignored
This question is a further development of this post and is different, although it may seem similar to this one .
I am trying to override QHeaderView::paintSection
so that the background returned from the model will be honored. I tried to do this
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>());
}
However, it didn't work - if I make a call QHeaderView::paintSection
, I do n't draw anything with the artist (apparently I was drawing a diagonal line too). If I remove the call QHeaderView::paintSection
, the line and background are visible. Making a call fillRect
to vs. after QHeaderView::paintSection
it doesn't matter.
I wonder what that means, QHeaderView::paintSection
making it impossible for me to do something on top of it. And is there a way to overcome this without rethinking everything it does QHeaderView::paintSection
?
All I have to do is add a specific shade to a specific cell - I still want everything in the cell (text, icons, gradient background, etc.) to be painted the way it is now ...
source to share
It's obvious why the first one fillRect
doesn't work. Anything you write before paintSection
is overridden by the base picture.
The second challenge is more interesting.
Usually all drawing methods are stateful painter
. This means that when you call paint
it looks like the state of the artist hasn't changed.
Nevertheless, it QHeaderView::paintSection
spoils the animal state.
To work around the problem, you need to save and restore the state yourself:
void Header::paintSection(QPainter * painter, const QRect & rect, int logicalIndex) const
{
QVariant bg = model()->headerData(logicalIndex, Qt::Horizontal, Qt::BackgroundRole);
painter->save();
QHeaderView::paintSection(painter, rect, logicalIndex);
painter->restore();
if(bg.isValid())
painter->fillRect(rect, bg.value<QBrush>());
}
source to share