PDA

View Full Version : QItemDelegate paint() is not called



Cortex
10th January 2010, 20:30
Hi,

Am trying to subclass an item Delegate (to display images on QTableView cells),
I reimplemented QItemDelegate::paint() and QItemDelegate::sizeHint().
When I tested it, the function paint() is not called at all.
Here is my code



class ItemImageDelegate : public QItemDelegate
{
Q_OBJECT

public:
ItemImageDelegate(QObject*) ;

void paint(QPainter * painter, const QStyleOptionViewItem & option, const QModelIndex & index) const;
QSize sizeHint(const QStyleOptionViewItem & option, const QModelIndex & index) const;
};



ItemImageDelegate::ItemImageDelegate(QObject *parent = 0) : QItemDelegate(parent) {}


void ItemImageDelegate::paint(QPainter * painter, const QStyleOptionViewItem & option, const QModelIndex & index) const
{
qDebug()<<"Paint called";
}

QSize ItemImageDelegate::sizeHint(const QStyleOptionViewItem & option, const QModelIndex & index) const
{
qDebug()<<"Size Hint called";
}



// Main class
// setting the model ...
QTableView* view = new QTableView();
view->setModel(model);
ItemImageDelegate delegate(this);
view->setItemDelegate(&delegate);
view->resizeColumnsToContents();

view->show();


It seems so wierd to me, am for sure missing something, but I can't figure it out
Please help

Happy coding for all !!

wysota
10th January 2010, 20:53
You are creating your delegate on the stack so it goes out of scope and is destroyed. Create it on heap instead.

Cortex
10th January 2010, 21:03
Wysota you're a life saver :)