How to add Action specific index on Qtreeview ?
-
Hi @kshegunov
example :
Apple
---a
---bPear
---c
---dHow to make another context menu for Pear and How to make another context menu for Apple?
so Apple and Pear context menu will be different.
-
Hi @kshegunov
example :
Apple
---a
---bPear
---c
---dHow to make another context menu for Pear and How to make another context menu for Apple?
so Apple and Pear context menu will be different.
@takoo
Okay. You can intercept the context menu events for the tree view. Are you working with aQTreeView
subclass? Let's assume you are; you can override the QWidget::contextMenuEvent handler and process the event yourself. Then from the QContextMenuEvent you can retrieve the position in widget coordinates, and finally you can get theQModelIndex
that's corresponding to the item at that poistion through the QAbstractItemView::indexAt method. Here's a bit more information on the subject (it's an example from the docs). Something like this should suffice to get you started:class MyTreeView : public QTreeView { Q_OBJECT public: // ... protected: virtual void contextMenuEvent(QContextMenuEvent *) Q_DECL_OVERRIDE; }; void MyTreeView::contextMenuEvent(QContextMenuEvent * event) { QPoint position = event->pos(); QModelIndex itemIndex = indexAt(position); // ... Decide based on the model index what context menu you'll be showing ... }
Kind regards.
-
@kshegunov I did not understand anything. Please Can you give me example code (.cpp including)
-
@kshegunov I did not understand anything. Please Can you give me example code (.cpp including)
@takoo said:
Please Can you give me example code (.cpp including)
But I did, I sourced a full example from the Qt documentation with 4 files (at the bottom), the header, the sources and the project file.
Did you miss the link? Here it is:
http://doc.qt.io/qt-5/qtwidgets-mainwindows-menus-example.htmlKind regards.