QHeaderView::paintSection paints over widgets above?

  • I have a QTreeView that features a frozen column like explained in http://doc.qt.io/qt-5/qtwidgets-itemviews-frozencolumn-example.html. Basically, a second QTreeView stacked above a base QTreeView and only showing one column.

    The problem comes when I have to handle painting of the base QTreeView's QHeaderView myself (for a requested feature). When implementing QHeaderView::paintSection, it ends up painting over the overlying, frozen header. When I scroll the view now, it draws over the would-be frozen header.

    Blue: "frozen" QTreeView, Yellow: base QTreeView drawing its header over the frozen view's headers above.
    Blue: "frozen" QTreeView, Yellow: base QTreeView drawing its header over the frozen view's headers.

    Here's my paintSection:

    void FrozenTreeWidget::MySpecialHeaderView::paintSection( QPainter* in_p_painter, const QRect& in_rect, int in_section ) const
        QHeaderView::paintSection( in_p_painter, in_rect, in_section );
        // Paint something else on the column sections
        QRect my_rect( in_rect.right(), in_rect.top(), 1, in_rect.height() );
        in_p_painter->fillRect( my_rect, QColor( "green" ) );

    In my FrozenTreeWidget, I then assign it as the base view's header:

    MySpecialHeaderView* p_header = new MySpecialHeaderView( Qt::Horizontal, this );
    setHeader( p_header );

    I'm really stumped with this one.

  • Lifetime Qt Champion


    Can you provide a minimal compilable example that shows that behaviour ?

  • Alright, while constructing a compilable sample, I figured it out:
    I missed calling "stackUnder()" on my special header class and stacking it under the frozen table view.
    Just making the base table its parent, or stacking it under the frozen table's header does not work.

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.