Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HowTo draw pixmap in QHeaderView section using delegate?

Problem: I need to draw small pixmap to QHeaderView section like presented on this figure (pixmap located on the right corner of the section, marked with red square):

enter image description here

As I understand, there is two ways to do that:

  1. reimplement QHeaderView's paintSection() method.

  2. create an delegate from QStyledItemDelegate class and reimplement paint() method.

If I tried (1) variant with this code below, filter pixmap are not shown 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 );
          }
     }
}

The (2) variant is this:

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 ) );
}

Delegate created, but function did not start the paint() method!

Any help?

Thanks!

like image 223
mosg Avatar asked Feb 21 '23 15:02

mosg


1 Answers

It Seems like it isn't possible at this time.

From Qt documentation:

Note: Each header renders the data for each section itself, and does not rely on a delegate. As a result, calling a header's setItemDelegate() function will have no effect.

How to have dynamic pixmap in a QHeaderView item

But you may override QHeaderView::paintSection method. 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;
};
like image 103
bartolo-otrit Avatar answered Mar 03 '23 12:03

bartolo-otrit