Qt subclassed QStyledItemDelegate paint method is never called
Update: my paint method was not const
I have a custom QTableView
connected to a QAbstractTableModel
.
I recently created a QStyledItemDelegate
subclass and its paint
method prints a debugging message and then it calls the parent paint and then it prints another debugging message.
I have called view->setItemDelegate
and I have checked that the item delegate in my view is the one I gave it (just to be sure).
However, my program just renders things the same way it did before and never prints the debug message in paint
. It does print a debug message in the delegate's constructor, so I know it should be printing if that function ever gets called.
Here is a stripped down version of my code, let me know one of these:
- Can you see what is wrong?
- How I can debug Qt classes like this?
- What other relevant code I should post to diagnose this proble开发者_如何学Pythonm?
- Is there some common solution I could try?
Thanks!
Here is the entire delegate .h and .cpp files (minus includes/namespace)
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!";
}
So the debugging output tells me the constructor is called (but not the destructor...) and that I am actaully setting the same deleagate to the view as I think I am. However, the messages in paint
never get printed.
I think your problem is that you are not declaring your subclass's paint method as const. As such, it's not overriding the superclass method (const and non-const methods are considered to have different signatures from each other) and that's why your paint method doesn't get called.
精彩评论