QTreeView context menu item
-
At present I have a
QTreeView
, bound to a (derived)QAbstractItemModel
. I have a right-click context menu with aQAction
, whosetriggered
connects to a slot. In the slot I need to access which model item was right-clicked, but I don't think I can see that from the action/slot?How is this easiest achieved?
-
The only way I see is to as the QTreeView for the current index.
-
Hi,
Using the customContextMenuRequested signal you can get the index using QTreeView::indexAt.
-
@SGaist
Thanks, yes, I have seen that approach. And I may indeed change over. But that happens at the context-menu-requested stage, right? I need to know at theQAction
-slot-execute phase. So you want me to either:- Store up the item at the right-click phase, and apply that at the action-phase; or
- Pass the item at the right-click phase as a parameter to the action-slot phase
Is that right? Or am I going doolally?
The only way I see is to as [edit: "ask"] the QTreeView for the current index.
Thank you. I will investigate tomorrow. I am so tired today :(
-
IIRC, when you right click on an item, it becomes selected so you can use the QTreeView::selectedIndexes in your action's slot.
-
@SGaist
Tx so much, I will look tomorrowThe point is, as I think both you & @Christian-Ehrlicher are confirming, the
QAction
/slot has no concept of which "item" you were on? You either have to look up the item by converting a click coordinate position to an item (QTreeView::itemAt()
), or you have to look at whatever item is currently selected (QTreeView::selectedIndexes
). Is that right, that's what I need to know? -
AFAIK, yes.