Detecting clicks on QTreeView icons.
-
I have a tree view which i am customizing using delegates to create icon.
this is the Delegate class
class PixelDelegate : public QStyledItemDelegate { Q_OBJECT public: using QStyledItemDelegate::QStyledItemDelegate; void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const override; QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const override; QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const override; void setEditorData(QWidget *editor, const QModelIndex &index) const override; void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const override; void updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option , const QModelIndex &index) const override; };
which i am adding to the tree view.
PixelDelegate *delegate = new PixelDelegate(this); // For icons in tree view treeView->setItemDelegate(delegate); this is how the tree view looks. 
How can i get the click on the icon of the tree item ?
-
Hi
Do you draw the icon in "paint" yourself so you know the location ?
If yes, you can use
https://doc.qt.io/qt-5/qabstractitemdelegate.html#editorEventas it also is called for mouse events so you should be able to know if it was pressed and do check if
icons was hit. -
Hi
You should store the location of the icon in some variable so we can use it later.
When you paint it.
like QRect iconRect. As class member. in .h
Then set it in paint when you paint the icon.in EditorEvent
the pos() of the event give the mouse point where we clicked.So check would be ( pseudo code )
if (iconRect.contains( event->pos() )) ..we hit icon..
You might need to cast the event to MouseEvent to have pos().