4

問題: この図に示すように、セクションに小さなピックスマップを描画する必要がありますQHeaderView(セクションの右隅にあり、赤い四角でマークされているピックスマップ):

ここに画像の説明を入力

私が理解しているように、それを行うには2つの方法があります。

  1. 再実装QHeaderViewpaintSection()メソッド。

  2. クラスからデリゲートを作成し、メソッドQStyledItemDelegateを再実装しpaint()ます。

以下のコードで (1) バリアントを試した場合、フィルター 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()!

何か助けはありますか?

ありがとう!

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 に答える