4

问题:我需要将小像素图绘制到QHeaderView如图所示的部分(像素图位于部分的右上角,用红色方块标记):

在此处输入图像描述

据我了解,有两种方法可以做到这一点:

  1. 重新实现QHeaderViewpaintSection()方法。

  2. QStyledItemDelegate从类创建一个委托并重新实现paint()方法。

如果我尝试使用以下代码的 (1) 变体,则根本不显示过滤器像素图:

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

(2) 变体是这样的:

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

委托已创建,但函数未启动该paint()方法!

有什么帮助吗?

谢谢!

4

1 回答 1

8

看来目前是不可能的了。

来自 Qt 文档:

注意:每个标头都为每个部分本身呈现数据,并且不依赖于委托。因此,调用标头的 setItemDelegate() 函数将不起作用。

如何在 QHeaderView 项目中具有动态像素图

但是您可以覆盖 QHeaderView::paintSection 方法。 子类化-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;
};
于 2012-05-25T11:24:32.997 回答