How to draw pixmap in QHeaderView section using delegate?

Problem . I need to draw a small pixmap in the section QHeaderView, as shown in this figure (pixmap located in the right corner of the section, marked with a red square):

enter image description here

As I understand it, there are two ways to do this:

  • reimplement QHeaderView paintSection().

  • create a delegate from the class QStyledItemDelegateand override the method paint().

If I tried the (1) option with this code below, the pixmap filter does not appear at all:

void DecorativeHeaderView::paintSection( QPainter* painter, const QRect& rect, int logicalIndex ) const
{
     if( !rect.isValid() )
     {
          return;
     }

     // get the state of the section
     QStyleOptionHeader option;
     initStyleOption( &option );

     // setup the style options structure
     option.rect = rect;
     option.section = logicalIndex;
     option.iconAlignment = Qt::AlignVCenter | Qt::AlignHCenter;

     QVariant variant = model()->headerData( logicalIndex, orientation(), Qt::DecorationRole );
     option.icon = qvariant_cast< QIcon >( variant );
     if( option.icon.isNull() )
     {
          option.icon = qvariant_cast< QPixmap >( variant );
     }

     // draw the section
     if( !option.icon.isNull() )
     {
          style()->drawControl( QStyle::CE_Header, &option, painter, this );
     }
     else
     {
          QHeaderView::paintSection( painter, rect, logicalIndex );

// HERE is where I'm trying to draw my filter picture!!!

          if( logicalIndex == filteredLogicalIndex_ )
          {
               QPixmap pixmap( ":/spreadsheet/images/spreadsheet/filter_icon_table.png" );
               int x = rect.right() - pixmap.width();
               int y = rect.top() + ( rect.height() - pixmap.height() ) / 2;

               painter->drawPixmap( QPoint( x, y ), pixmap );
          }
     }
}

Option (2) is as follows:

class HeaderDelegate : public QStyledItemDelegate
{
     Q_OBJECT
     Q_DISABLE_COPY( HeaderDelegate )

public:
     HeaderDelegate( QObject* parent = 0 ) : QStyledItemDelegate( parent ) {}
     virtual ~HeaderDelegate() {}

     virtual void paint( QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index ) const;

}; // HeaderDelegate

void HeaderDelegate::paint( QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index ) const
{

// THIS method never starts!!!

     if( index.column() == 2 )
     {
          QPixmap pixmap( ":/spreadsheet/images/spreadsheet/filter_icon_table.png" );
          int x = option.rect.right() - pixmap.width();
          int y = option.rect.top() + ( option.rect.height() - pixmap.height() ) / 2;

          painter->save();
          painter->drawPixmap( QPoint( x, y ), pixmap );
          painter->restore();
     }

     QStyledItemDelegate::paint( painter, option, index );
}

DecorativeHeaderView::DecorativeHeaderView( Qt::Orientation orientation, QWidget* parent /* = 0 */ )
     : QHeaderView( orientation, parent )
     , filteredLogicalIndex_( -1 )
{
     setItemDelegate( new HeaderDelegate( this ) );
}

The delegate is created, but the function does not start the method paint()!

Any help?

Thank!

+3
source share
1 answer

It seems that this is not possible at this time.

From the Qt documentation:

. . header setItemDelegate() .

pixmap QHeaderView

QHeaderView:: paintSection. subclassing-QHeaderView

class HeaderView : public QHeaderView {
    Q_OBJECT

public:
    HeaderView(Qt::Orientation orientation, QWidget * parent = 0)
        : QHeaderView(orientation, parent), p("333222.jpeg") {
    }

    int sectionSizeHint ( int /*logicalIndex*/ ) const { return p.width(); }

protected:
    void paintSection(QPainter * painter, const QRect & rect, int /*logicalIndex*/) const {
        painter->drawPixmap(rect, p);
    }

private:
    QPixmap p;
};
+7

All Articles