1

Ok, this question has been asked before - but I didn't find an answer yet..

I'm trying to left/right swipe between images using a QLabel but I didn't manage to receive any gesture events yet with my PyQt5 based application. Here is what I have:

class MyViewer(QtWidgets.QLabel):
    def __init__(self, parent=None):
        super().__init__(parent=parent)
        self.setAttribute(QtCore.Qt.WA_AcceptTouchEvents)  # just gave it a try
        self.grabGesture(QtCore.Qt.SwipeGesture)

    def event(self, event):
        if event.type() == QtCore.QEvent.Gesture:  # <= this won't happen
            print("Hello event!")
        return super().event(event)

I also tried to look for QtWidgets.QGestureEvent but even when I just print out event.type() I only see events seemingly unrelated to any gestures (except some QToucheEvent instances)

Similar questions are this one, this one, this one and I even read some documentation but I didn't see a working example yet. Only questions and example C++ snippets..

I'm using PyQt5.15.0 for both Linux and Android and I didn't see a single trace of anything gesture-ish yet. What did I forget to do? Does the parent have to be set up somehow?

frans
  • 8,868
  • 11
  • 58
  • 132

1 Answers1

0

How many fingers were you swiping with? According to the Qt forum, Qt.SwipeGesture is only registered when swiping with two or more fingers, depending on the platform. One finger is represented as a QtGui.QMouseEvent.

If you have been using two or more fingers, it is also possible that the reason that you saw no events is because you did not explictly accept them. The docs say:

A QGesture is by default accepted when it arrives at your widget. However, it is good practice to always explicitly accept or reject a gesture. The general rule is that, if you accept a gesture, you are using it. If you are ignoring it you are not interested in it. Ignoring a gesture may mean it gets offered to another target object, or it will get canceled. [...] Ignoring the gesture will mean the gesture will never be offered to you again.

Umbral Reaper
  • 325
  • 4
  • 9