問題:私は(ピックスマップがセクションの右隅にあり、赤い四角でマークされた)この図に示さようQHeaderView
セクションに小さなピックスマップを描画する必要があります。デリゲートを使ってQHeaderViewセクションでピックスマップを描画するには?
私は理解しているように、
再実装
QHeaderView
のpaintSection()
方法:それを行うには、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()
メソッドを開始しませんでした。
助けが必要ですか?
ありがとうございます!
。あなたはそれを使用しようとしましたか? –
@vaychick: 'setItemDelegate()'と 'setItemDelegateForColumn()'の主な違いはありません。このメソッドを実行する際に問題はありません。何か他のものがあります... – mosg