2012-05-04 10 views
3

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

enter image description here

私は理解しているように、

  1. 再実装QHeaderViewpaintSection()方法:それを行うには、2つの方法があります。

  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()メソッドを開始しませんでした。

助けが必要ですか?

ありがとうございます!

+0

。あなたはそれを使用しようとしましたか? –

+0

@vaychick: 'setItemDelegate()'と 'setItemDelegateForColumn()'の主な違いはありません。このメソッドを実行する際に問題はありません。何か他のものがあります... – mosg

答えて

7

これは現時点では不可能なようです。 Qtのドキュメントから

注:各ヘッダは、各セクション自体のデータをレンダリングし、デリゲートに依存しません。その結果、ヘッダのsetItemDelegate()関数を呼び出すことは効果がありません。

How to have dynamic pixmap in a QHeaderView item

しかし、あなたはQHeaderView :: paintSectionメソッドをオーバーライドすることができます。私は私のプロジェクトでQAbstractItemView :: setItemDelegateForColumnメソッドを使用し 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; 
}; 
+0

返信ありがとうございます。私は 'paintSection()'をチェックします... – mosg

+0

一般的にあなたの例は動作しますが、 'sectionSizeHint'は効果がありません(少なくともQt 5.5+では)。 – Gluttton

関連する問題