【问题标题】:QListView doesn't use size hint of delegateQListView 不使用委托的大小提示
【发布时间】:2011-08-02 16:14:14
【问题描述】:

我有一个呈现自定义项委托的 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()(我已在调用检查中添加了调试语句),因此项目的大小不正确。任何人都可以看到可能是什么问题?

【问题讨论】:

    标签: qt user-interface listview delegates model-view


    【解决方案1】:

    这是因为签名不匹配。您错过了签名末尾的const(滚动代码)。

    应该是

    QSize CardItemDelegate::sizeHint ( const QStyleOptionViewItem & option, const QModelIndex & index ) const
                                                                                                       //^^^^^ - here
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多