-1

I am subclassing QHeaderView to add a filtering icon in the horizontal header of a QTableView. The QTableView has sorting capability activated consume a QSortFilterProxyModel, until now it works fine. However when I try to subclass QHeaderView and use it as column header, only the first column shows the filter icon.

headerview_filter.h

#ifndef HEADERVIEW_FILTER_H
#define HEADERVIEW_FILTER_H

#include <QHeaderView>

class HeaderView_Filter : public QHeaderView
{
Q_OBJECT
public:
   explicit HeaderView_Filter(Qt::Orientation orientation, QWidget * parent = nullptr);
   void paintSection(QPainter *painter, const QRect &rect, int logicalIndex) const override;

private:
   const QPolygonF _funel = QPolygonF({{22.0,36.0},{22.0,22.0},{10.0,10.0},{40.0,10.0},{28.0,22.0},{28.0,36.0}});
};

#endif // HEADERVIEW_FILTER_H

headerview_filter.cpp

#include "headerview_filter.h"

HeaderView_Filter::HeaderView_Filter(Qt::Orientation orientation, QWidget * parent)
: QHeaderView(orientation, parent)
{
   setSectionsClickable(true);
}

void HeaderView_Filter::paintSection(QPainter *painter, const QRect &rect, int logicalIndex) const
{
   painter->save();
   QHeaderView::paintSection(painter, rect, logicalIndex);
   painter->restore();
   const double scale = 0.6*rect.height()/50.0;
   painter->setBrush(Qt::black);
   painter->translate(0,5);
   painter->scale(scale, scale);
   painter->drawPolygon(_funel);
   painter->restore();
}

using it in form :

auto* tableView = _ui->tableView_Data;    
tableView->setModel(_sortFilterProxyModel);
tableView->setSortingEnabled(true);
tableView->setHorizontalHeader(new HeaderView_Filter(Qt::Horizontal,tableView));
user2019716
  • 587
  • 4
  • 13

1 Answers1

0

I found the solution while typing it and prefered to post the code for other to use. The position of the drawing must be translated relative to the drawing rectangle provided as argument of paintSection :

void HeaderView_Filter::paintSection(QPainter *painter, const QRect &rect, int logicalIndex) const
{
   painter->save();
   QHeaderView::paintSection(painter, rect, logicalIndex);
   painter->restore();
   const double scale = 0.6*rect.height()/50.0;
   painter->setBrush(Qt::black);
   // Here
   painter->translate(rect.x(),rect.y()+5);
   //
   painter->scale(scale, scale);
   painter->drawPolygon(_funel);
   painter->restore();
}
user2019716
  • 587
  • 4
  • 13