Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [Solved] Qt 4.8: Reverse primary and secondary mouse buttons application wide

[Solved] Qt 4.8: Reverse primary and secondary mouse buttons application wide

Scheduled Pinned Locked Moved General and Desktop
8 Posts 4 Posters 3.0k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    teomurgi
    wrote on last edited by
    #1

    Hi all,

    is it possible to reverse primary and secondary mouse buttons in the whole application
    upon user request?

    thank you

    1 Reply Last reply
    0
    • raven-worxR Offline
      raven-worxR Offline
      raven-worx
      Moderators
      wrote on last edited by
      #2

      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.

      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
      If you have a question please use the forum so others can benefit from the solution in the future

      1 Reply Last reply
      0
      • Chris KawaC Offline
        Chris KawaC Offline
        Chris Kawa
        Lifetime Qt Champion
        wrote on last edited by
        #3

        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?

        1 Reply Last reply
        0
        • B Offline
          B Offline
          b1gsnak3
          wrote on last edited by
          #4

          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)

          1 Reply Last reply
          0
          • raven-worxR Offline
            raven-worxR Offline
            raven-worx
            Moderators
            wrote on last edited by
            #5

            [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.

            --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
            If you have a question please use the forum so others can benefit from the solution in the future

            1 Reply Last reply
            0
            • T Offline
              T Offline
              teomurgi
              wrote on last edited by
              #6

              [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:

              1. 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.
              2. 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.

              1 Reply Last reply
              0
              • T Offline
                T Offline
                teomurgi
                wrote on last edited by
                #7

                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!

                1 Reply Last reply
                0
                • T Offline
                  T Offline
                  teomurgi
                  wrote on last edited by
                  #8

                  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

                  1 Reply Last reply
                  0

                  • Login

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • Users
                  • Groups
                  • Search
                  • Get Qt Extensions
                  • Unsolved