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

后端 未结 1 530
一向
一向 2020-12-06 22:25

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

相关标签:
1条回答
  • 2020-12-06 22:39

    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>());             
    }
    
    0 讨论(0)
提交回复
热议问题