PDA

View Full Version : Disable right mouse click on QAction



stefan
14th December 2010, 11:05
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

high_flyer
14th December 2010, 11:11
See QWidget::customContextMenuRequested ().
Or in the widget you are right clicking on you can overload the mousePressEvent().

stefan
14th December 2010, 19:41
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:

void MainWindow::on_pushButton_customContextMenuRequest ed(QPoint pos)
{
QMenu menu;
QAction* closeAction = new QAction(tr("Close"),this);
connect(closeAction , SIGNAL(triggered()), this, SLOT(close()));
menu.addAction(closeAction );
menu.exec();
}

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!

solay
15th December 2010, 02:14
exactly what i am looking for ...

high_flyer
15th December 2010, 08:35
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.

stefan
15th December 2010, 09:08
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

lllturtle
14th April 2011, 04:26
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

#ifndef CONTEXTMENU_H
#define CONTEXTMENU_H

#include <QMenu>
#include <QMouseEvent>

class contextMenu : public QMenu
{
Q_OBJECT

public:
contextMenu(QWidget *parent = 0);

signals:
// void leftMouseClicked();

protected:
void mousePressEvent(QMouseEvent *event)
{
if(event->button() == Qt::LeftButton)
event->accept();
// else
// event->ignore();
// emit leftMouseClicked();
// emit triggered(QAction *action);
}
void mouseReleaseEvent(QMouseEvent *event)
{
if(event->button() == Qt::LeftButton)
event->accept();
// else
// event->ignore();
// emit leftMouseClicked();
// emit triggered(QAction *action);
}

};


#endif // CONTEXTMENU_H

contextMenu.cpp // nothing actually

#include <contextMenu.h>

contextMenu::contextMenu(QWidget *parent)
: QMenu(parent)
{
}

In test_paint.cpp

// show menu when right button clicked
void test_paint::contextMenuEvent(QContextMenuEvent *event)
{
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 = new QAction(tr("&Rr"), this);
rrAct->setCheckable(true);
connect(rrAct, SIGNAL(triggered()), this, SLOT(rr()));

testAct = new QAction(tr("&Test"), this);
testAct->setCheckable(true);
connect(testAct, SIGNAL(triggered()), this, SLOT(test()));
}

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"

void test_paint::test()
{
l_msg->setText("test button selected");
}
void test_paint::rr()
{
l_msg->setText("rr button selected");
}

high_flyer
14th April 2011, 09:20
the implementation fo rr() and test().

lllturtle
15th April 2011, 02:26
hi high_flyer ,
i've added rr() and test() in #7 post , thanks

high_flyer
15th April 2011, 08:47
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"?

Archa4
15th April 2011, 09:06
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...

high_flyer
15th April 2011, 09:14
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.

lllturtle
15th April 2011, 11:20
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 :(

muby
15th May 2013, 22:04
I know it's an old post, but I have used the solution of high_flyer and it work well :




[...]
QMenu * menu = new QMenu(0);
menu->installEventFilter(this);
[...]

//-----------------------------------------------------------------------
bool MyClass::eventFilter( QObject * obj, QEvent * event )
{
bool val= QObject::eventFilter(obj, event);

QMenu * menu = dynamic_cast<QMenu*>(obj);
if(menu && event->type() == QEvent::MouseButtonPress)
{
QMouseEvent * ev = dynamic_cast<QMouseEvent*>(event);
if(ev)
{
if(ev->button() == Qt::RightButton)
{
ev->ignore();
return true; // yes we filter the event
}
}
}
return val;
}