PDA

View Full Version : QItemDelegate clickable icon



migel
19th June 2012, 14:50
I draw a text and the icon using QItemDelegate. Any ideas how can I make the icon clickable, like mouse over on specific rect to show the mouse hand and once clicked there emit some action.

Could you point me to some good direction please ?


virtual void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
if ( option.state & QStyle::State_Selected )
painter->fillRect(option.rect, option.palette.highlight());

else if ( option.state & QStyle::State_MouseOver )
painter->fillRect(option.rect, option.palette.highlight());

QPixmap pixmap(":icons/icon.png");
QRect iconRect = option.rect;

iconRect.setTop( iconRect.top() );
iconRect.setLeft( iconRect.width() );

painter->drawPixmap(QPoint(iconRect.left(), iconRect.top()), pixmap);

QRect textRect = option.rect;
QString text = qvariant_cast<QString>(index.data(Qt::DisplayRole));
painter->drawText( textRect, text );
}

wysota
20th June 2012, 10:58
You need to handle events within QAbstractItemDelegate::editorEvent().