0

カスタム アイテム デリゲートをレンダリングする 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()呼び出されることはありません (チェックする呼び出しにデバッグ ステートメントを追加しました)。そのため、アイテムのサイズが正しくありません。何が問題なのか誰にもわかりますか?

4

1 に答える 1

3

署名の不一致が原因です。const署名の最後 (スクロール コード) を見逃しました。

する必要があります

QSize CardItemDelegate::sizeHint ( const QStyleOptionViewItem & option, const QModelIndex & index ) const
                                                                                                   //^^^^^ - here
于 2011-08-02T16:37:18.990 に答える