How to call contextMenuEvent on Qt::Key_F5? I want to display popup menu on Qt::Key_F5.
Printable View
How to call contextMenuEvent on Qt::Key_F5? I want to display popup menu on Qt::Key_F5.
Well,
You don't need to call contextMenuEvent to display a context menu.
When you catch Qt::Key_F5 in keyPressEvent(), you just have to build a QMenu, and exec it at the cursor's current position ( QCursor::pos() )
Maybe there are another ways though ;-)
Thanks! its working.
But..
I have a QTreeWidget and if any tree item is selected and user pressed F5 key then popup menu should be displyed.
problem is, if tree item is selected and mouse pointer is somewhere else then QCursor::Pos() displaying popup menu at mouse pointer position not tree item position.
how to display popup menu at selected tree item position?
Thanks & Regards
Rajesh
I don't think there's a direct method to obtain position of an item...
Maybe this would work : when you select an item by clicking, save the cursor position in a variable...and if you call context menu with the cursor away, then use the saved position (assuming you haven't selected anything else :-) )
You can get the geometry of the current/selected item by QTreeWidget::visualItemRect(). Popup the menu for example at the preferred corner of the item (remember to map the coordinate to the global coordinate system, see QWidget::mapToGlobal()).
That's the good way :)