C++ 从未调用Qt子类QStyledItemDelegate绘制方法
更新:我的绘制方法不是常量 我有一个自定义的C++ 从未调用Qt子类QStyledItemDelegate绘制方法,c++,debugging,qt,model-view,C++,Debugging,Qt,Model View,更新:我的绘制方法不是常量 我有一个自定义的QTableView连接到QAbstractTableModel 我最近创建了一个QStyledItemDelegate子类,它的paint方法打印一条调试消息,然后调用父paint,然后打印另一条调试消息 我调用了view->setItemDelegate,并检查了视图中的item委托是否就是我给它的(只是为了确定) 但是,我的程序只是以与以前相同的方式渲染,从不在paint中打印调试消息。它确实在委托的构造函数中打印调试消息,因此我知道如果调用该函
QTableView
连接到QAbstractTableModel
我最近创建了一个QStyledItemDelegate
子类,它的paint
方法打印一条调试消息,然后调用父paint,然后打印另一条调试消息
我调用了view->setItemDelegate
,并检查了视图中的item委托是否就是我给它的(只是为了确定)
但是,我的程序只是以与以前相同的方式渲染,从不在paint
中打印调试消息。它确实在委托的构造函数中打印调试消息,因此我知道如果调用该函数,它应该打印
这是我的代码的精简版本,请告诉我其中一个:
类QtCellItemDelegate:公共QStyledItemDelegate
{
Q_对象;
公众:
QtCellItemDelegate(QWidget*parent=NULL);
空漆(QPainter*油漆工、,
常量QSTYLEOPTION视图项和选项,
常数QModelIndex和索引);
QSize sizeHint(常量QStyleOptionViewItem和option,
常数QModelIndex&index)常数;
虚拟~qtcellitemdegate();
};
QtCellItemDelegate::QtCellItemDelegate(QWidget*父项):
QStyledItemDelegate(父级)
{
qDebug()保存();
qDebug()我认为您的问题在于您没有将子类的paint方法声明为const。因此,它没有覆盖超类方法(const和non-const方法被认为具有彼此不同的签名)这就是为什么你的paint方法没有被调用。是的,我一个小时前就知道了……可惜这个项目明天早上就要到期了。至少我几乎按时完成了。不过谢谢你的回答!@Matt:如果你使用Visual Studio或兼容C++11的编译器,你可以使用override
keyowrd来确保你的C正确地重写虚拟方法。如果您使用Visual Studio编译并且另外在Linux/Mac上编译,只需使用宏即可。
class QtCellItemDelegate : public QStyledItemDelegate
{
Q_OBJECT;
public:
QtCellItemDelegate( QWidget *parent = NULL );
void paint( QPainter *painter,
const QStyleOptionViewItem &option,
const QModelIndex &index );
QSize sizeHint( const QStyleOptionViewItem &option,
const QModelIndex & index ) const;
virtual ~QtCellItemDelegate();
};
QtCellItemDelegate::QtCellItemDelegate( QWidget *parent ) :
QStyledItemDelegate( parent )
{
qDebug() << "CONSTRUCT";
}
void QtCellItemDelegate::paint( QPainter *painter,
const QStyleOptionViewItem &option,
const QModelIndex &index )
{
painter->save();
qDebug() << "Begin Paint";
QStyledItemDelegate::paint( painter, option, index );
qDebug() << " Begin custom paint";
// omitted...
qDebug() << "End paint";
painter->restore();
}
QSize QtCellItemDelegate::sizeHint( const QStyleOptionViewItem &option,
const QModelIndex & index ) const
{
return QSize( 60, 60 );
}
QtCellItemDelegate::~QtCellItemDelegate()
{
qDebug() << "Destroy delegate!";
}