QListView不使用委托的大小提示
我有一个呈现自定义项目委托的 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());
项目在列表视图中正确显示,但是 < code>sizeHint() 从未被调用(我已在调用中添加了调试语句以进行检查),因此项目的大小不正确。有人能看出可能是什么问题吗?
I have a QListView that renders custom item delegates. I am overriding sizeHint()
of the delegate to provide the size but it seems the list view doesn't take this into account. Below is the code I'm using:
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();
}
And this is how I'm using it:
DeckListModel* model = new DeckListModel();
ui->deckListView->setModel(model);
ui->deckListView->setItemDelegate(new CardItemDelegate());
The items are displayed properly in the list view however sizeHint()
is never called (I've added a debug statement to the call to check) so the items don't have the right size. Can anybody see what could be the issue?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是因为签名不匹配。您错过了签名末尾的
const
(滚动代码)。应该是
it's because of signature mismatch. You missed
const
at the end of the signatute (scroll code).Should be