【问题标题】:HowTo draw pixmap in QHeaderView section using delegate?如何使用委托在 QHeaderView 部分中绘制像素图?
【发布时间】:2012-05-04 12:01:21
【问题描述】:

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

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

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

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

如果我尝试使用以下代码的 (1) 变体,则根本不会显示 filter pixmap

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() 方法!

有什么帮助吗?

谢谢!

【问题讨论】:

  • 我在我的项目中使用 QAbstractItemView::setItemDelegateForColumn 方法。你试过用吗?
  • @vaychick:我没有t see any principal differences between setItemDelegate()` 和setItemDelegateForColumn(),问题不在于执行此方法。还有一点……

标签: qt delegates qt4 qheaderview


【解决方案1】:

目前看来是不可能的。

来自 Qt 文档:

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

How to have dynamic pixmap in a QHeaderView item

但是你可以重写 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;
};

【讨论】:

  • 一般来说你的例子是有效的,但是sectionSizeHint 没有效果(至少对于Qt 5.5+)。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2022-06-10
  • 1970-01-01
  • 1970-01-01
  • 2020-11-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多