0

I want to use events generated by Windows 10 touchpad gestures in QT (C++, Visual Studio):

  • two finger pinch on touchpad for zoom
  • two finger pan on touchpad for panning

The QT documentation / QTCreator has an example for gestures. Apparently, the method shown using the QGesture class only works for touchscreens. I tested that straight on the example without any of my own code.

I suspect that QNativeGestureEvent somehow can do what I want, but there are no examples.

Can anyone confirm that QNativeGestureEvent is the way to solve my task?

eyllanesc
  • 235,170
  • 19
  • 170
  • 241

2 Answers2

0

You need to grab the gestures. Check this document for more details. In general the documentation on this topic has improved but it's still far from complete.

Once you enable the gestures you just need to process the events. You can also implement your own gestures but for that you need custom gesture events and gesture recognition (the documentation here is nonexistent since custom gesture recognition in itself is out of the scope of Qt). I've done this with a simple mouse so it shouldn't be much more difficult. Here is what I have. Note that the code is from 3 years ago so things might have changed. Also it's not perfect and if I remember correctly there were some errors but it should be a good start. I developed it for adding mouse gestures (using the trackpad of my notebook) to a widget.

I would check what events the touchpad produces. Qt is more than capable of processing multiple simultaneous touch events (otherwise gestures such as zoom and pan wouldn't be possible) so it all boils down to what Qt can get from a touchpad in terms of data.

rbaleksandar
  • 8,713
  • 7
  • 76
  • 161
  • Thanks for the code, I compiled this, but it doesn't react to touch pad input. I think this is for touch*screen* input. – Vinzent Meier May 31 '20 at 10:09
  • I wrote the code for the trackpad on my back then Sony Vaio notebook. I posted it here so that you can get a general idea of processing gestures. You need to adapt it to your scenario and device. – rbaleksandar May 31 '20 at 13:48
  • Thanks for the clarification! I'll look into that! – Vinzent Meier May 31 '20 at 15:33
0

So I found a sort of acceptable solution for my problem:

Minimal example:

header:

class MyWidget : public QLabel {
// in this case: QLabel is base class

// ... constructors etc...


protected:
    void wheelEvent(QWheelEvent *) override;
};

cpp

void MyWidget::wheelEvent(QWheelEvent *e)
{
    if (e->modifiers() & Qt::ControlModifier) {
        qDebug() << "zoom " << e->angleDelta().y();
    }
    else
    {
        qDebug() << "pan x " << e->angleDelta().x();
        qDebug() << "pan y " << e->angleDelta().y();
    }

    e->accept();
}

This gives x and y values for panning and zoom values for pinching the touchpad respectively. The limination with this approach is, that it gives only values in one axis while panning, i.e. x or y is zero; this is acceptable for now.

I had no luck using the Gestures approach; I understand that these are meant for touchscreens and not touchpads. My computer has no touchscreen to test this.