Results 1 to 14 of 14

Thread: Disable right mouse click on QAction

  1. #1
    Join Date
    May 2008
    Location
    Rijeka, Croatia
    Posts
    85
    Thanks
    10
    Thanked 6 Times in 6 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Disable right mouse click on QAction

    hi,
    how can I disable triggered event on right button click?
    when a right click for context menu it often triggers first action in context menu.
    it's very annoying.

    Thank you

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Disable right mouse click on QAction

    See QWidget::customContextMenuRequested ().
    Or in the widget you are right clicking on you can overload the mousePressEvent().
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  3. #3
    Join Date
    May 2008
    Location
    Rijeka, Croatia
    Posts
    85
    Thanks
    10
    Thanked 6 Times in 6 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Disable right mouse click on QAction

    maybe I explained wrong.
    i dont want trigger action on right mouse button click.
    my problem is cheap mouse so i often do double right click:
    • first raises context menu
    • second triggers first action in context menu

    you can't trigger action from toolbar or main menu with right click, but you can trigger action from custom context menu with right click. how to change that?


    EXAMPLE:
    Qt Code:
    1. void MainWindow::on_pushButton_customContextMenuRequested(QPoint pos)
    2. {
    3. QMenu menu;
    4. QAction* closeAction = new QAction(tr("Close"),this);
    5. connect(closeAction , SIGNAL(triggered()), this, SLOT(close()));
    6. menu.addAction(closeAction );
    7. menu.exec();
    8. }
    To copy to clipboard, switch view to plain text mode 
    • when i right click on button - context menu is shown
    • when i right click on "close" - window is cosed. i dont want that! only on left click!

  4. #4
    Join Date
    Dec 2010
    Posts
    1
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows Symbian S60

    Default Re: Disable right mouse click on QAction

    exactly what i am looking for ...

  5. #5
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Disable right mouse click on QAction

    my problem is cheap mouse so i often do double right click:
    Let me get this straight: you have a defective mouse, and you want to work it around with software???
    Even if you do, it only will fix this behavior in your application, you will still have that problem everywhere else.
    Mice are really cheap these days, you can get a decent one for little money.

    Never the less:
    The answer is the same.
    You can subclass QMenu, and override its mousePressEvent() to trigger the action only on left clicks.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  6. The following user says thank you to high_flyer for this useful post:

    stefan (15th December 2010)

  7. #6
    Join Date
    May 2008
    Location
    Rijeka, Croatia
    Posts
    85
    Thanks
    10
    Thanked 6 Times in 6 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Disable right mouse click on QAction

    Mouse was just mechanism which found the problem
    On other computers i didn't notice that behavior.
    ok, subclassing QMenu.. i thought there is some flag or something to do this simpler...
    Thanks

  8. #7
    Join Date
    Mar 2011
    Posts
    20
    Thanks
    6
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Question Re: Disable right mouse click on QAction

    Quote Originally Posted by high_flyer View Post
    You can subclass QMenu, and override its mousePressEvent() to trigger the action only on left clicks.
    Hello every one,
    i've the same problem with stefan , but not the chip mouse
    i want to disable the function of right triggered action ,
    but i can't find a way to solve that , following is my problem code
    contextMenu.h
    Qt Code:
    1. #ifndef CONTEXTMENU_H
    2. #define CONTEXTMENU_H
    3.  
    4. #include <QMenu>
    5. #include <QMouseEvent>
    6.  
    7. class contextMenu : public QMenu
    8. {
    9. Q_OBJECT
    10.  
    11. public:
    12. contextMenu(QWidget *parent = 0);
    13.  
    14. signals:
    15. // void leftMouseClicked();
    16.  
    17. protected:
    18. void mousePressEvent(QMouseEvent *event)
    19. {
    20. if(event->button() == Qt::LeftButton)
    21. event->accept();
    22. // else
    23. // event->ignore();
    24. // emit leftMouseClicked();
    25. // emit triggered(QAction *action);
    26. }
    27. void mouseReleaseEvent(QMouseEvent *event)
    28. {
    29. if(event->button() == Qt::LeftButton)
    30. event->accept();
    31. // else
    32. // event->ignore();
    33. // emit leftMouseClicked();
    34. // emit triggered(QAction *action);
    35. }
    36.  
    37. };
    38.  
    39.  
    40. #endif // CONTEXTMENU_H
    To copy to clipboard, switch view to plain text mode 
    contextMenu.cpp // nothing actually
    Qt Code:
    1. #include <contextMenu.h>
    2.  
    3. contextMenu::contextMenu(QWidget *parent)
    4. : QMenu(parent)
    5. {
    6. }
    To copy to clipboard, switch view to plain text mode 
    In test_paint.cpp
    Qt Code:
    1. // show menu when right button clicked
    2. void test_paint::contextMenuEvent(QContextMenuEvent *event)
    3. {
    4. if(y < window_h){
    5. // QMenu menu(this);
    6. contextMenu menu(this);
    7. menu.addAction(rrAct);
    8. menu.addAction(testAct);
    9. menu.exec(event->globalPos());
    10. }
    11. }
    12. void test_paint::createActions()
    13. {
    14. rrAct = new QAction(tr("&Rr"), this);
    15. rrAct->setCheckable(true);
    16. connect(rrAct, SIGNAL(triggered()), this, SLOT(rr()));
    17.  
    18. testAct = new QAction(tr("&Test"), this);
    19. testAct->setCheckable(true);
    20. connect(testAct, SIGNAL(triggered()), this, SLOT(test()));
    21. }
    To copy to clipboard, switch view to plain text mode 
    now i'm running this program and pressed right button ,
    but i can't do anything after contextMenu displayed on screen

    can anyone give a more complete sample about
    "How to reimplement QMenu"
    any help will be appreciated!

    Thanks

    hello high_flyer ,
    first , thanks for your reply ,

    // l_msg is a label , after the two button pressed , just show some string
    // it was working properly , but i want to disable the "right button triggered"
    Qt Code:
    1. void test_paint::test()
    2. {
    3. l_msg->setText("test button selected");
    4. }
    5. void test_paint::rr()
    6. {
    7. l_msg->setText("rr button selected");
    8. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by lllturtle; 14th April 2011 at 09:40.

  9. #8
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Disable right mouse click on QAction

    the implementation fo rr() and test().
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  10. #9
    Join Date
    Mar 2011
    Posts
    20
    Thanks
    6
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Disable right mouse click on QAction

    hi high_flyer ,
    i've added rr() and test() in #7 post , thanks

  11. #10
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Disable right mouse click on QAction

    now i'm running this program and pressed right button ,
    but i can't do anything after contextMenu displayed on screen
    Can you explain more what do you mean by "can't do anything"?

    // l_msg is a label , after the two button pressed , just show some string
    // it was working properly , but i want to disable the "right button triggered"
    I really have hard time following what you are saying.
    You are programming a context menu to be shown on right click.
    The menu shows, you select an action, and the slot for that action is executed, and you get the label with the text shown.
    So it looks what you want is working.
    Then what do you mean when you say you want to disable the "right button triggered"?
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  12. #11
    Join Date
    Feb 2011
    Location
    Latvia
    Posts
    139
    Thanks
    24
    Thanked 6 Times in 6 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Disable right mouse click on QAction

    I think he wants to achieve following:
    You press RMB to bring up context menu. Now the RMB STOPS working (you cannot select any option with RMB).
    His mouse often does double RMB click (brings up the context menu and activates first option in it), and he wants to disable that possibility...

  13. #12
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Disable right mouse click on QAction

    His mouse often does double RMB click (brings up the context menu and activates first option in it), and he wants to disable that possibility...
    So he wants to fix a hardware problem (mouse not behaving right) with a software solution?
    Bad idea.

    So if the question is - "how do I disable right click for menus" the answer is:
    1. install event filetr on the menu and ignore mouse press events for the right button.
    2. Subclass QMenu override the mousePressEvent() and ignore there the right button.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  14. #13
    Join Date
    Mar 2011
    Posts
    20
    Thanks
    6
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Disable right mouse click on QAction

    hi ,
    both of you got it , but not at all ,
    my mouse is working properly , after reading other post on google ,
    i found that it's about OS , i was coding in windows and it's running properly
    (the RMB show contextMenu after mouseReleaseEvent),
    but in linux (the RMB show contextMenu after mousePressEvent) ,
    and if mouse move carelessly the action will select after mouseReleaseEvent ,
    lead to user may get the wrong select action
    ( mousePress -> show contextMenu -> mouseMoveToAction carelessly -> mouseRelease -> running action )
    so i want to disable the RMB like Archa4 sayed ,
    and i've try to Subclass the QMenu , but in vain ,
    don't know how to reimplement mousePressEvent() ,
    the qmenu.cpp do something i can't figure out ! Orz

    Can you explain more what do you mean by "can't do anything"?
    after contextMenu showed (rr and test showed), my mouse borken no matter LMB or RMB .

    thanks for your help
    and sorry for my poor english

  15. #14
    Join Date
    Aug 2008
    Posts
    2
    Thanked 1 Time in 1 Post

    Thumbs up Re: Disable right mouse click on QAction

    I know it's an old post, but I have used the solution of high_flyer and it work well :

    Qt Code:
    1. [...]
    2. QMenu * menu = new QMenu(0);
    3. menu->installEventFilter(this);
    4. [...]
    5.  
    6. //-----------------------------------------------------------------------
    7. bool MyClass::eventFilter( QObject * obj, QEvent * event )
    8. {
    9. bool val= QObject::eventFilter(obj, event);
    10.  
    11. QMenu * menu = dynamic_cast<QMenu*>(obj);
    12. if(menu && event->type() == QEvent::MouseButtonPress)
    13. {
    14. QMouseEvent * ev = dynamic_cast<QMouseEvent*>(event);
    15. if(ev)
    16. {
    17. if(ev->button() == Qt::RightButton)
    18. {
    19. ev->ignore();
    20. return true; // yes we filter the event
    21. }
    22. }
    23. }
    24. return val;
    25. }
    To copy to clipboard, switch view to plain text mode 

  16. The following user says thank you to muby for this useful post:


Similar Threads

  1. Can't disable a QAction in QMenu
    By punkypogo in forum Qt Programming
    Replies: 3
    Last Post: 10th August 2010, 14:07
  2. How to disable mouse click over a QSplashScreen
    By graciano in forum Qt Programming
    Replies: 2
    Last Post: 8th November 2009, 16:02
  3. Replies: 3
    Last Post: 25th August 2009, 22:35
  4. Disable QTextCursor Mouse click repositioning
    By VireX in forum Qt Programming
    Replies: 2
    Last Post: 3rd April 2007, 08:08
  5. Replies: 1
    Last Post: 9th February 2007, 09:41

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.