What does QHeaderView::paintSection do such that all I do to the painter before or after is ignored
Asked Answered
L

1

5

This question is further development of this post and is different, though may seem similar as this one.

I am trying to reimplement QHeaderView::paintSection, so that the background returned from the model would 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 QHeaderView::paintSection call, nothing I draw with the painter is visible (I also tried drawing a diagonal line). If I remove QHeaderView::paintSection call, the line and the background will be visible. Making the fillRect call before vs. after the QHeaderView::paintSection doesn't make any difference.

I wonder, what is it that QHeaderView::paintSection does that makes it impossible for me to draw something on top of it. And whether there is a way to overcome it without reimplementing everythning what QHeaderView::paintSection does?

All I need to do is to add a certain shade to a certain cell - I still want everything in the cell (text, icons, gradient background etc.) to be painted as it is now...

Ladysmith answered 15/6, 2015 at 14:5 Comment(6)
What would bg.value<QBrush>() return ? is it a valid QBrush ?Hardden
The model would either return a QBrush or an empty QVariant (if no custom background is needed). bg.isValid takes care of the later. So, yes - bg.value<QBrush>() returns the valid brush. And if I comment the QHeaderView::paintSection call, I can see the rect being correctly filledLadysmith
The QHeaderView::paintSection is drawing on top of your rectangle... it may be covering it ... try changing the order of the callsHardden
I tried both before and after, same result - nothing shows upLadysmith
I tried both before and after - updated code sample - neither worksLadysmith
Can you post a picture of how you want it to look like and what are you getting instead in both cases?Faa
G
11

It is obvious why the first fillRect doesn't work. Everything that you paint before paintSection is overridden by base painting.

The second call is more interesting.

Usually all paint methods preserves painter state. It means that when you call paint it looks like the painter state hasn't been changed.

Nevertheless QHeaderView::paintSection spoils the painter state.

To bypass the issue you need to save and restore the state by 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>());             
}
Guileful answered 17/6, 2015 at 9:52 Comment(1)
How can I implement this. Do I need a .h and .cpp for the above? What code do I need in main() to color my headers?Korrie

© 2022 - 2024 — McMap. All rights reserved.