Results 1 to 2 of 2

Thread: Prevent QContextMenuEvent being sent because of right click

  1. #1
    Join Date
    Oct 2009
    Posts
    5
    Thanks
    1

    Default Prevent QContextMenuEvent being sent because of right click

    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.

  2. #2
    Join Date
    Oct 2010
    Posts
    55
    Thanks
    1
    Thanked 11 Times in 10 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    9

    Default Re: Prevent QContextMenuEvent being sent because of right click

    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:

    Qt Code:
    1. void MainWindow::contextMenuEvent(QContextMenuEvent* event)
    2. {
    3. switch (event->reason()) {
    4. case QContextMenuEvent::Mouse:
    5. event->accept();
    6. return;
    7. default:
    8. QMenu menu;
    9. // ...
    10. menu.exec(mapToGlobal(event->pos()));
    11. }
    12. }
    To copy to clipboard, switch view to plain text mode 

  3. The following user says thank you to helloworld for this useful post:

    Donner (26th February 2011)

Similar Threads

  1. Replies: 6
    Last Post: 5th June 2009, 09:38
  2. QContextMenuEvent appear on mouse release
    By bucksey in forum Qt Programming
    Replies: 1
    Last Post: 21st March 2009, 01:09
  3. Replies: 2
    Last Post: 11th January 2009, 23:24
  4. QGraphicsScene Click / Double Click
    By philentropist in forum Qt Programming
    Replies: 1
    Last Post: 9th February 2007, 04:32
  5. Replies: 5
    Last Post: 12th January 2006, 15:40

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.