PDA

View Full Version : How to call contextMenuEvent on Qt::Key_F5?



rajesh
12th January 2007, 11:35
How to call contextMenuEvent on Qt::Key_F5? I want to display popup menu on Qt::Key_F5.

guilugi
12th January 2007, 11:39
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 ;-)

rajesh
12th January 2007, 13:52
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



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 ;-)

guilugi
12th January 2007, 14:38
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 :-) )

jpn
12th January 2007, 14:42
how to display popup menu at selected tree item position?
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()).

guilugi
12th January 2007, 14:44
That's the good way :)