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. QWheelEvent - can I "hijack" or change the mouse coordinates in the event?
Qt 6.11 is out! See what's new in the release blog

QWheelEvent - can I "hijack" or change the mouse coordinates in the event?

Scheduled Pinned Locked Moved General and Desktop
2 Posts 2 Posters 831 Views 2 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.
  • P Offline
    P Offline
    pmh4514
    wrote on last edited by
    #1

    I'm sure this will sound like a "why would you want to do that?" question, but in this particular context which I won't waste time describing, it is desired.

    I want to trap the mouseWheelEvent and change the contained mouse coordinates before passing it on to the parent widget.

    sub-classing and implementing mouseWheelEvent isn't the question, rather, simply, how can I modify the cursor coordinates before passing the event along?

    Thanks

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

      Why would you want to do that? No, seriously, I'm curious ;)

      As for how - well, one way is to install an event filter on the target object, block the real event and substitute it for your own.
      Something like this:

      bool MyFilterObject::eventFilter(QObject* obj, QEvent* evt)
      {
          if(evt->type() == QEvent::Wheel && !dynamic_cast<MyCustomEvent*>(evt)) { //avoid recursion
              QWheelEvent* whe = static_cast<QWheelEvent*>(evt);
      
              QPointF pt(42.0f, 42.0f);
              QPointF globalPt(42.0f, 42.0f);
              QWheelEvent* myNewEvent = new MyCustomEvent(pt, globalPt, whe); //craft new event
      
              QApplication::instance()->postEvent(obj, myNewEvent); //post it to the receiver
              return true; //block the original
          }
          return false; //pass anything else freely
      }
      

      A thing to note here is that you can't just post another regular wheel event because it would recurse infinitely.
      So one way to solve it is to make your own event type derived from QWheelEvent and check for that. the receiver won't know the difference but you will. It can be a simple copy of the original with only the data you want to change customized:

      struct MyCustomEvent : public QWheelEvent {
          MyCustomEvent(const QPointF& pos, const QPointF& globalPos, QWheelEvent* src): QWheelEvent(
              pos,
              globalPos,
              src->pixelDelta(),
              src->angleDelta(),
              src->delta(),
              src->orientation(),
              src->buttons(),
              src->modifiers(),
              src->phase(),
              src->source()
          ) {}
      };
      
      1 Reply Last reply
      2

      • Login

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