Is there a way to make MacOS convert the mouse events to a touch events?

Viewed 23

I have a Mac with macOS 10.15.7. I also have this touch screen connected to it: https://www.usa.philips.com/c-p/242B9T_27/monitor-lcd-monitor-with-smoothtouch

I tried installing a universal driver from these guys https://www.touch-base.com/ (trial version -- so no support from them.).

The touch screen works but it's converting the screen touches to mouse events. I used this example project from Qt to verify that: https://code.qt.io/cgit/qt/qtbase.git/tree/examples/widgets/touch/fingerpaint?h=6.3

Is there a way to make MacOS convert the mouse events to a touch events? Failing that, is there a way to intercept Qt mouseEvents and convert them to touch events? Or does someone know a better driver? -- I couldn't find one.

1 Answers

It is not a good idea to mess with the Qt event handling yourself. The reason why it does not seem to do touch events for you is because you have to explicitly enable this in your Qt application.

Please refer to the following page for details.

To receive touch events, widgets have to have the Qt::WA_AcceptTouchEvents attribute set and graphics items need to have the acceptTouchEvents attribute set to true.

And then, you can reimplement QObject::event(QEvent *e).

You can find the event types here where you will also find the touch event type to handle.

QEvent::TouchBegin Beginning of a sequence of touch-screen or track-pad events (QTouchEvent).

QEvent::TouchCancel Cancellation of touch-event sequence (QTouchEvent).

QEvent::TouchEnd End of touch-event sequence (QTouchEvent).

QEvent::TouchUpdate Touch-screen event (QTouchEvent).

So, to put it all into practice, you would write something like this:

MyWidget::MyWidget(QWidget *parent) : QWidget(parent)
{
   ...
   setAttribute(Qt::WA_AcceptTouchEvents);
   ...
}

or just this outside the class definition:

myWidget->setAttribute(Qt::WA_AcceptTouchEvents);

And then, you actually handle touch events:

bool MyWidget::event(QEvent* event)
{
    switch (event->type()) {
    case QEvent::TouchUpdate:
        // Handle
        break;
    default:
        break;
        
    }
    return QObject::event(event);
}
Related