[Solved] Qt 4.8: Reverse primary and secondary mouse buttons application wide
-
you could subclass QApplication and override "QCoreApplication::notify()":http://qt-project.org/doc/qt-4.8/qcoreapplication.html#notify
In there you catch all MouseButton-Press and -Release events and substitute them.
But this may be a bit tricky. -
I'm not gonna help much, sorry, but I'm a little intrigued if you don't mind - why would you want to do something like that? Isn't that up to users and OS wide settings what the particular mouse buttons do? Why do you want that on an application level?
-
[quote author="b1gsnak3" date="1366098285"]You could reimplement mousePressEvent for your widgets, or install an event filter to your application in which you switch the events (if leftClick -> make it rightClick event)[/quote]
This won't work as expected in most of the cases. Consider the event propagation:
When the event is handed to the parent how does the parent widget know if it's a correct LMB/RMB click or a substituted one? That's even the case when installing an eventFilter on the QApplication object. You will most probably receive the same even multiple times since not every widget consumes the mosue event and thus the event is passed to the parent.
That's why i suggested - if he really wants to go this way - the reimplementation of the QApplication::notify() method. Since this will also work correctly with even propagation. -
[quote author="Chris Kawa" date="1366051975"]I'm not gonna help much, sorry, but I'm a little intrigued if you don't mind - why would you want to do something like that? Isn't that up to users and OS wide settings what the particular mouse buttons do? Why do you want that on an application level?[/quote]
I need this because of two reasons:
- I'm developing a kiosk style application in which the OS must be completely hidden by the application. This application can have more than one user and it is a requirement to support left handed people.
- I must be as much as independent from the underlying OS as possible
[quote author="raven-worx" date="1366098691"]
This won't work as expected in most of the cases. Consider the event propagation:[/quote]
That is exactly my problem right now, I'm going to try with the "notify" solution as soon as possible and let you know. Thanks for the help.
-
Just tried with this quick code:
@bool STApplication::notify(QObject* receiver, QEvent* event){
QEvent* newEvent = event;
qDebug() << event;
if(event->type()== QEvent::MouseButtonPress || event->type()== QEvent::MouseButtonRelease || event->type()== QEvent::MouseButtonDblClick){QMouseEvent *mouseEvent = static_cast<QMouseEvent *>(event);
if(mouseEvent->button()==Qt::LeftButton){
QMouseEvent *temp = new QMouseEvent (
mouseEvent->type(),
mouseEvent->pos(),
Qt::RightButton,Qt::RightButton,mouseEvent->modifiers() );
event->setAccepted(true);
newEvent = temp;
}else if(mouseEvent->button()==Qt::RightButton){
QMouseEvent *temp = new QMouseEvent (
mouseEvent->type(),
mouseEvent->pos(),
Qt::LeftButton,Qt::LeftButton,mouseEvent->modifiers() );
event->setAccepted(true);
newEvent = temp;
}
}return QApplication::notify(receiver,newEvent);
}@What happens is that the mouse buttons are correctly inverted.... but the QContextMenuEvent is still fired when the right button is pressed!
-
Ok, it seems that I managed it:
@
bool STApplication::notify(QObject* receiver, QEvent* event){QEvent* newEvent = event;
if(event->type()== QEvent::MouseButtonPress || event->type()== QEvent::MouseButtonRelease || event->type()== QEvent::MouseButtonDblClick){
event->setAccepted(true);
QMouseEvent *mouseEvent = static_cast<QMouseEvent *>(event);
if(mouseEvent->button()==Qt::LeftButton){
newEvent = new QMouseEvent (
mouseEvent->type(),
mouseEvent->pos(),
Qt::RightButton,Qt::RightButton,mouseEvent->modifiers() );
QApplication::sendEvent(receiver,new QContextMenuEvent(QContextMenuEvent::Keyboard,mouseEvent->pos()));
}else if(mouseEvent->button()==Qt::RightButton){
newEvent = new QMouseEvent (
mouseEvent->type(),
mouseEvent->pos(),
Qt::LeftButton,Qt::LeftButton,mouseEvent->modifiers() );
}
}else if(event->type()== QEvent::ContextMenu){
QContextMenuEvent *cmEvent = static_cast<QContextMenuEvent *>(event);
if(cmEvent->reason()==QContextMenuEvent::Mouse){
cmEvent->accept();
return true;
}
}return QApplication::notify(receiver,newEvent);
}
@As you can see, in case of "left button" i send a QContextMenuEvent too, with the peculiarity that its reason is Keyboard.
This is the trick, as a matter of fact now I can filter all the QContextMenuEvent that are "Mouse" generated.thanks again