PDA

View Full Version : Prevent QContextMenuEvent being sent because of right click



Donner
22nd February 2011, 18:25
Hey,

is there a way of disabling certain triggers that cause the QContextMenuEvent being sent to a widget?

For instance, I don't want the context menu to open when the user presses the right mouse button, but I do want it to open when the context-menu-key on the keyboard is being pressed.
How can I do that?

Thank you very much for your help! :)
D.

helloworld
22nd February 2011, 22:54
If you reimplement QWidget::contextMenuEvent; event->reason() returns the QContextMenuEvent::Reason value telling you if the event was caused by the mouse, keyboard or something else. Then you can respond accordingly:



void MainWindow::contextMenuEvent(QContextMenuEvent* event)
{
switch (event->reason()) {
case QContextMenuEvent::Mouse:
event->accept();
return;
default:
QMenu menu;
// ...
menu.exec(mapToGlobal(event->pos()));
}
}