カスタム アイテム デリゲートをレンダリングする QListView があります。サイズを提供するためにデリゲートをオーバーライドsizeHint()
していますが、リスト ビューではこれが考慮されていないようです。以下は私が使用しているコードです:
CardItemDelegate.h
#ifndef CARDITEMDELEGATE_H
#define CARDITEMDELEGATE_H
class CardItemDelegate : public QStyledItemDelegate {
Q_OBJECT
public:
explicit CardItemDelegate(QObject *parent = 0);
QSize sizeHint(const QStyleOptionViewItem& option, const QModelIndex& index);
void paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const;
};
#endif // CARDITEMDELEGATE_H
CardItemDelegate.cpp
#include "CardItemDelegate.h"
CardItemDelegate::CardItemDelegate(QObject *parent) : QStyledItemDelegate(parent) {
}
QSize CardItemDelegate::sizeHint(const QStyleOptionViewItem& option, const QModelIndex& index) {
qDebug() << "size hint called";
return QSize(100, 30);
}
void CardItemDelegate::paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const {
painter->save();
painter->setBrush(Qt::green);
painter->setPen(Qt::red);
painter->drawRect(option.rect);
painter->restore();
}
そして、これが私がそれを使用している方法です:
DeckListModel* model = new DeckListModel();
ui->deckListView->setModel(model);
ui->deckListView->setItemDelegate(new CardItemDelegate());
アイテムはリスト ビューに正しく表示されますが、sizeHint()
呼び出されることはありません (チェックする呼び出しにデバッグ ステートメントを追加しました)。そのため、アイテムのサイズが正しくありません。何が問題なのか誰にもわかりますか?