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. Changing QMouseEvent position
Qt 6.11 is out! See what's new in the release blog

Changing QMouseEvent position

Scheduled Pinned Locked Moved Solved General and Desktop
11 Posts 4 Posters 5.1k Views 3 Watching
  • 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.
  • mrjjM Offline
    mrjjM Offline
    mrjj
    Lifetime Qt Champion
    wrote on last edited by
    #2

    Hi
    Can you explain a bit what is the use case for this ?
    It sounds a bit strange so not sure i understand. :)

    So you have a windows MainWindow with a button on, and in case you click some where else on the MainWindow
    you want the button to get the MouseButtonPress anyway ?
    Regardless of where you click ?

    You could use
    http://doc.qt.io/qt-5/qwidget.html#grabMouse
    and all mouse events go to button until you release it but im not sure
    that is what you want.

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mpergand
      wrote on last edited by mpergand
      #3

      Hi,

      I think you need to send a mouse press event yourself.
      But it seems there's more simple :

      [slot] void QAbstractButton::click()

      Performs a click.
      All the usual signals associated with a click are emitted as appropriate. If the button is checkable, the state of the button is toggled.

      1 Reply Last reply
      1
      • etiennedmE Offline
        etiennedmE Offline
        etiennedm
        wrote on last edited by
        #4

        Thank you for you replies !

        The idea behind is a plan B in case I do not succeed in calibrating my touchscreen with my screen (sizes are different).
        So I need to catch all mouse's events in my app, perform a transformation for each (the calibration) and send new events with a different position. I although need catch events to be ignored by my app which should only received "calibrated events".
        My app contains different buttons and pages, so I would need something generic.

        @mpergand : When you say I think you need to send a mouse press event yourself, do you know how I can first make them ignored by all my QPushButton in my app ? That would be a nice first step ! After that, I may use this

        bool QCoreApplication::sendEvent(QObject * receiver, QEvent * event)
        

        to send my custom event

        Thank you for helping !

        mrjjM 1 Reply Last reply
        0
        • etiennedmE etiennedm

          Thank you for you replies !

          The idea behind is a plan B in case I do not succeed in calibrating my touchscreen with my screen (sizes are different).
          So I need to catch all mouse's events in my app, perform a transformation for each (the calibration) and send new events with a different position. I although need catch events to be ignored by my app which should only received "calibrated events".
          My app contains different buttons and pages, so I would need something generic.

          @mpergand : When you say I think you need to send a mouse press event yourself, do you know how I can first make them ignored by all my QPushButton in my app ? That would be a nice first step ! After that, I may use this

          bool QCoreApplication::sendEvent(QObject * receiver, QEvent * event)
          

          to send my custom event

          Thank you for helping !

          mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by
          #5

          @etiennedm
          Hi
          Ah, make more sense now.
          I really hope you can calibrate the screen.

          Anyway, i think overriding
          http://doc.qt.io/qt-5/qcoreapplication.html#notify
          would allow this - as this function will see any event before distributed.

          1 Reply Last reply
          1
          • etiennedmE Offline
            etiennedmE Offline
            etiennedm
            wrote on last edited by
            #6

            @mrjj
            Thanks for pointing that out !
            I will look into this and try to make it work on a test example.

            J.HilkJ 1 Reply Last reply
            0
            • etiennedmE etiennedm

              @mrjj
              Thanks for pointing that out !
              I will look into this and try to make it work on a test example.

              J.HilkJ Offline
              J.HilkJ Offline
              J.Hilk
              Moderators
              wrote on last edited by
              #7

              @etiennedm
              I think, you're on the right track for your situations.

              If you override the top most (mainWindow) Qwidget::event of your app you can calculate your offset position and detect transfer the event along via

              QWidget::childAt
              and
              QCoreApplication::sendEvent

              something along this

              bool MainWindow::event(QEvent *event)
              {
                  if(event->type() == QEvent::MouseButtonPress || event->type() == QEvent::MouseButtonRelease || event->type() == QEvent::MouseMove) 
                  {
                      QMouseEvent *mouseEvent = static_cast<QMouseEvent*>(event);
              
                      const QPoint newPos(mouseEvent->x() - 50, mouseEvent->y() - 50);
                      QWidget *w = childAt(newPos);
              
                      if(w)
                         return QCoreApplication::sendEvent(qObject_cast<QObject *>(w), event);
                     else
                          return true;
                  } 
              }
              

              Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


              Q: What's that?
              A: It's blue light.
              Q: What does it do?
              A: It turns blue.

              1 Reply Last reply
              1
              • etiennedmE Offline
                etiennedmE Offline
                etiennedm
                wrote on last edited by
                #8

                Thank you all ! Problem solved !

                I used a custom class inherited from QApplication to override notify as suggested by @mrjj
                I'm able to change the event before it is sent to the entire app, thanks !
                Code here :

                bool MyQApplication::notify(QObject *receiver, QEvent *event)
                {
                    if(event->type() == QEvent::MouseButtonPress || event->type() == QEvent::MouseButtonRelease || event->type() == QEvent::MouseMove)
                    {
                        QMouseEvent *mouseEvent = static_cast<QMouseEvent*>(event);
                        qDebug() << "Mouse event detected (" << event->type() << ")- x=" << mouseEvent->x() << " - y=" << mouseEvent->y();
                
                        const QPoint newPos(mouseEvent->x() - 50, mouseEvent->y() - 50);
                        mouseEvent->setLocalPos(newPos);
                
                        return QApplication::notify(receiver, mouseEvent);
                    }
                    else
                    {
                        return QApplication::notify(receiver, event);
                    }
                }
                

                Thanks,

                Etienne

                J.HilkJ 1 Reply Last reply
                0
                • etiennedmE etiennedm

                  Thank you all ! Problem solved !

                  I used a custom class inherited from QApplication to override notify as suggested by @mrjj
                  I'm able to change the event before it is sent to the entire app, thanks !
                  Code here :

                  bool MyQApplication::notify(QObject *receiver, QEvent *event)
                  {
                      if(event->type() == QEvent::MouseButtonPress || event->type() == QEvent::MouseButtonRelease || event->type() == QEvent::MouseMove)
                      {
                          QMouseEvent *mouseEvent = static_cast<QMouseEvent*>(event);
                          qDebug() << "Mouse event detected (" << event->type() << ")- x=" << mouseEvent->x() << " - y=" << mouseEvent->y();
                  
                          const QPoint newPos(mouseEvent->x() - 50, mouseEvent->y() - 50);
                          mouseEvent->setLocalPos(newPos);
                  
                          return QApplication::notify(receiver, mouseEvent);
                      }
                      else
                      {
                          return QApplication::notify(receiver, event);
                      }
                  }
                  

                  Thanks,

                  Etienne

                  J.HilkJ Offline
                  J.HilkJ Offline
                  J.Hilk
                  Moderators
                  wrote on last edited by J.Hilk
                  #9

                  @etiennedm
                  great!

                  But you'll have to modfy that code of yours a bit.
                  I don't think that it would- currently - change to other objects, if your touch event is at the border of 2 objects and the offset would push the event to the next one.

                  I might be wrong, or it might also depend on the z-order of the widgets, I'm unsure.


                  Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                  Q: What's that?
                  A: It's blue light.
                  Q: What does it do?
                  A: It turns blue.

                  1 Reply Last reply
                  0
                  • etiennedmE Offline
                    etiennedmE Offline
                    etiennedm
                    wrote on last edited by
                    #10

                    @J-Hilk Thank for helping me on this.
                    Could you provide some details on what you're pointing out ?

                    The test I ran seems to work well for me (maybe I mis-explained what I wanted to achieve), so when I click aside a button, I can "click" it adjusting the offset (overriding notify). When I click on it (native cursor on it), nothing happens. (as long as the offset is enough to go outside the button)

                    I have some code running (largely based on this) if you want.

                    Thanks !

                    Etienne

                    J.HilkJ 1 Reply Last reply
                    0
                    • etiennedmE etiennedm

                      @J-Hilk Thank for helping me on this.
                      Could you provide some details on what you're pointing out ?

                      The test I ran seems to work well for me (maybe I mis-explained what I wanted to achieve), so when I click aside a button, I can "click" it adjusting the offset (overriding notify). When I click on it (native cursor on it), nothing happens. (as long as the offset is enough to go outside the button)

                      I have some code running (largely based on this) if you want.

                      Thanks !

                      Etienne

                      J.HilkJ Offline
                      J.HilkJ Offline
                      J.Hilk
                      Moderators
                      wrote on last edited by
                      #11

                      @etiennedm
                      I'm more thinking along the following line.

                      you have 2 QPushButtons next to each other, seperated by (let's say) 10 px. The left one goes a page back and the right one a pge forwards ( in example a QStackedWidget).

                      Now you have an offset of 50 in x direction.

                      Now you press the touch screen, your calculation pushes the new postion to the forwards button, but physically QCoreApplication registered the back button as receiver object.

                      As a result, your touchinput is ignored.


                      Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                      Q: What's that?
                      A: It's blue light.
                      Q: What does it do?
                      A: It turns blue.

                      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