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
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
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.
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:
{ QMenu menu; connect(closeAction , SIGNAL(triggered()), this, SLOT(close())); menu.addAction(closeAction ); menu.exec(); }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!
Let me get this straight: you have a defective mouse, and you want to work it around with software???my problem is cheap mouse so i often do double right click:
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.
stefan (15th December 2010)
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
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
contextMenu.cpp // nothing actuallyQt Code:
#ifndef CONTEXTMENU_H #define CONTEXTMENU_H #include <QMenu> #include <QMouseEvent> { Q_OBJECT public: signals: // void leftMouseClicked(); protected: { if(event->button() == Qt::LeftButton) event->accept(); // else // event->ignore(); // emit leftMouseClicked(); // emit triggered(QAction *action); } { if(event->button() == Qt::LeftButton) event->accept(); // else // event->ignore(); // emit leftMouseClicked(); // emit triggered(QAction *action); } }; #endif // CONTEXTMENU_HTo copy to clipboard, switch view to plain text mode
In test_paint.cppQt Code:
#include <contextMenu.h> { }To copy to clipboard, switch view to plain text mode
now i'm running this program and pressed right button ,Qt Code:
// show menu when right button clicked { if(y < window_h){ // QMenu menu(this); contextMenu menu(this); menu.addAction(rrAct); menu.addAction(testAct); menu.exec(event->globalPos()); } } void test_paint::createActions() { rrAct->setCheckable(true); connect(rrAct, SIGNAL(triggered()), this, SLOT(rr())); testAct->setCheckable(true); connect(testAct, SIGNAL(triggered()), this, SLOT(test())); }To copy to clipboard, switch view to plain text mode
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:
void test_paint::test() { l_msg->setText("test button selected"); } void test_paint::rr() { l_msg->setText("rr button selected"); }To copy to clipboard, switch view to plain text mode
Last edited by lllturtle; 14th April 2011 at 09:40.
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.
hi high_flyer ,
i've added rr() and test() in #7 post , thanks
Can you explain more what do you mean by "can't do anything"?now i'm running this program and pressed right button ,
but i can't do anything after contextMenu displayed on screen
I really have hard time following what you are saying.// 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"
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.
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...
So he wants to fix a hardware problem (mouse not behaving right) with a software solution?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...
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.
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
after contextMenu showed (rr and test showed), my mouse borken no matter LMB or RMB .Can you explain more what do you mean by "can't do anything"?
thanks for your help
and sorry for my poor english
I know it's an old post, but I have used the solution of high_flyer and it work well :
Qt Code:
[...] menu->installEventFilter(this); [...] //----------------------------------------------------------------------- { { if(ev) { if(ev->button() == Qt::RightButton) { ev->ignore(); return true; // yes we filter the event } } } return val; }To copy to clipboard, switch view to plain text mode
Bookmarks