Python / PySide6 - swipe gesture?
-
Hi
Does anyone have a reasonable example of capturing / processing a swipe gesture event within a pyside6/python program that I can use? (I've got a main window created using Designer, containing a number of widgets, including buttons, list and WebEngine view.
I've been googling/trying all morning and seem to have gotten nowhere... :-/
Using Python3.14 / Pyside 6.11
Many Thanks
Carl. -
Hi and welcome to devnet,
Not a Python example but did you consider translation the Image Gestures example ?
-
Hi - Yep - I've had a look at that - but I couldn't get that one to work under python either - I had a similar problem with PyQt6/Python3.9 - I ended up coding my own swipe recognition routine with mousepress-mousemove-mouserelease - I've updated my code to be latest release using pyside - and at this point it looks like I'm going to end up doing the same thing - I would have preferred to do it the 'proper' way if poss... I even got example code generated by AI, and while it looked sensible, it didn't recognise the gesture... I'm definately missing something...
-
I've ended up installing event filter and capturing the mousebutton or touchevents and position start/end to work out the swipe...
The annoying thing is that when I update the html, I have to re-install the eventfilter otherwise it won't respond to touch events... weird...
In the init:
```
self.SwipeStatus=0
self.SwipeXStart=0
self.SwipeYStart=0
self.SwipeXEnd=0
self.SwipeYEnd=0self.SongText.focusProxy().installEventFilter(self)then: ``` def eventFilter(self, source, event): if event.type() == QEvent.Type.MouseButtonPress or event.type() == QEvent.Type.MouseMove or event.type() == QEvent.Type.MouseButtonRelease or event.type() == QEvent.Type.TouchBegin or event.type() == QEvent.Type.Leave or event.type() == QEvent.Type.TouchUpdate or event.type() == QEvent.Type.TouchEnd or event.type() == QEvent.Type.Enter: if event.type() == QEvent.Type.Leave: # print ("Noise: Leave") a=0 if event.type() == QEvent.Type.TouchUpdate: # print ("Noise: TouchUpdate") a=0 if event.type() == QEvent.Type.Enter: # print ("Noise: Enter") a=0 if event.type() == QEvent.Type.MouseButtonPress or event.type() == QEvent.Type.TouchBegin: self.SwipeStatus=1 if event.type() == QEvent.Type.MouseButtonPress: self.SwipeXStart=event.globalPosition().x() self.SwipeYStart=event.globalPosition().y() else: self.SwipeXStart=event.point(0).position().x() self.SwipeYStart=event.point(0).position().y() self.SwipeXEnd=0 self.SwipeYEnd=0 if event.type() == QEvent.Type.MouseMove: # dummy a=0 if event.type() == QEvent.Type.MouseButtonRelease or event.type() == QEvent.Type.TouchEnd: if self.SwipeStatus == 1: if event.type() == QEvent.Type.MouseButtonRelease: self.SwipeXEnd=event.globalPosition().x() self.SwipeYEnd=event.globalPosition().y() else: self.SwipeXEnd=event.point(0).position().x() self.SwipeYEnd=event.point(0).position().y() if abs(self.SwipeXEnd-self.SwipeXStart) > abs(self.SwipeYEnd-self.SwipeYStart): # Horizontal if self.SwipeXStart < self.SwipeXEnd: print("Right to Left") else: print("Left to Right") else: print("Vertical") if self.SwipeYStart < self.SwipeYEnd: print("Down") else: print("Up") self.SwipeStatus=0 else: self.SwipeStatus=0 else: self.SwipeStatus=0 return super().eventFilter(source, event) -
I've ended up installing event filter and capturing the mousebutton or touchevents and position start/end to work out the swipe...
The annoying thing is that when I update the html, I have to re-install the eventfilter otherwise it won't respond to touch events... weird...
In the init:
```
self.SwipeStatus=0
self.SwipeXStart=0
self.SwipeYStart=0
self.SwipeXEnd=0
self.SwipeYEnd=0self.SongText.focusProxy().installEventFilter(self)then: ``` def eventFilter(self, source, event): if event.type() == QEvent.Type.MouseButtonPress or event.type() == QEvent.Type.MouseMove or event.type() == QEvent.Type.MouseButtonRelease or event.type() == QEvent.Type.TouchBegin or event.type() == QEvent.Type.Leave or event.type() == QEvent.Type.TouchUpdate or event.type() == QEvent.Type.TouchEnd or event.type() == QEvent.Type.Enter: if event.type() == QEvent.Type.Leave: # print ("Noise: Leave") a=0 if event.type() == QEvent.Type.TouchUpdate: # print ("Noise: TouchUpdate") a=0 if event.type() == QEvent.Type.Enter: # print ("Noise: Enter") a=0 if event.type() == QEvent.Type.MouseButtonPress or event.type() == QEvent.Type.TouchBegin: self.SwipeStatus=1 if event.type() == QEvent.Type.MouseButtonPress: self.SwipeXStart=event.globalPosition().x() self.SwipeYStart=event.globalPosition().y() else: self.SwipeXStart=event.point(0).position().x() self.SwipeYStart=event.point(0).position().y() self.SwipeXEnd=0 self.SwipeYEnd=0 if event.type() == QEvent.Type.MouseMove: # dummy a=0 if event.type() == QEvent.Type.MouseButtonRelease or event.type() == QEvent.Type.TouchEnd: if self.SwipeStatus == 1: if event.type() == QEvent.Type.MouseButtonRelease: self.SwipeXEnd=event.globalPosition().x() self.SwipeYEnd=event.globalPosition().y() else: self.SwipeXEnd=event.point(0).position().x() self.SwipeYEnd=event.point(0).position().y() if abs(self.SwipeXEnd-self.SwipeXStart) > abs(self.SwipeYEnd-self.SwipeYStart): # Horizontal if self.SwipeXStart < self.SwipeXEnd: print("Right to Left") else: print("Left to Right") else: print("Vertical") if self.SwipeYStart < self.SwipeYEnd: print("Down") else: print("Up") self.SwipeStatus=0 else: self.SwipeStatus=0 else: self.SwipeStatus=0 return super().eventFilter(source, event)@CarlB said in Python / PySide6 - swipe gesture?:
I've ended up installing event filter and capturing the mousebutton or touchevents and position start/end to work out the swipe...
The intended usage is to register a gesture recognizer.
The annoying thing is that when I update the html, I have to re-install the eventfilter otherwise it won't respond to touch events...
How are you installing the event filter?
What operating system, windowing system, and hardware are you using? A look at the source for QGestureManager shows different recognizers for macOS based platforms versus the default case.
Also, are you able to trigger any other gestures?
Voluminous gesture debugging:
from PyQt6.QtWidgets import QApplication, QWidget from PyQt6.QtCore import QEvent, QLoggingCategory, Qt class Widget(QWidget): def event(self, e: QEvent) -> bool: if QEvent.Type.Gesture == e.type(): print(f"Gesture delivered: {e.gestures()}") return super().event(e) app = QApplication([]) QLoggingCategory.setFilterRules("qt.widgets.gestures=true") w = Widget() for gesture in (Qt.GestureType.TapGesture, Qt.GestureType.TapAndHoldGesture, Qt.GestureType.PanGesture, Qt.GestureType.PinchGesture, Qt.GestureType.SwipeGesture): w.grabGesture(gesture) w.show() app.exec()On a mac, I have only been able to cause a
TapAndHoldGesturewith a mouse. With a touch pad,PanGestureis also easily triggered. I haven't had any luck so far withPinchGestureorSwipeGesture. The gesture manager source linked above tells me not to expectTapGesture. -
Hi
Ok - While the test program generates loads of output, unfortunately its still not recognising a swipe... I did note that your example is using PyQT6, I hadn't mentioned that I'm using PySide6 - however, I believe that they should be compatible.As I'm only interested in swipes, I removed the other gesture types from the list, so I peared down the program to:
from PySide6.QtWidgets import QApplication, QWidget from PySide6.QtCore import QEvent, QLoggingCategory, Qt class Widget(QWidget): def event(self, e: QEvent) -> bool: if QEvent.Type.Gesture == e.type(): print(f"Gesture delivered: {e.gestures()}") return super().event(e) app = QApplication([]) QLoggingCategory.setFilterRules("qt.widgets.gestures=true") w = Widget() w.grabGesture(Qt.GestureType.SwipeGesture) w.show() app.exec()Meanwhile - I opened the program, did a very quick swipe right to left, and here's the output:
qt.widgets.gestures: QGestureManager:Recognizer: maybe gesture: QSwipeGesture(state=NoGesture,horizontalDirection=Right,verticalDirection=NoDirection,swipeAngle=0) QTouchEvent(TouchBegin device: "" states: Pressed, 1 points: QList(QEventPoint(id=0 ts=92554578 pos=429.5,265 scn=829.5,481 gbl=829.5,481 Pressed pressure=0.5 ellipse=(65.5x84.5 ? 0) vel=0,0 press=429.5,265 last=429.5,265 ? 0,0))) qt.widgets.gestures: QGestureManager::filterEventThroughContexts: activeGestures: QSet() maybeGestures: QSet(QSwipeGesture(state=NoGesture,horizontalDirection=Right,verticalDirection=NoDirection,swipeAngle=0)) started: QSet() triggered: QSet() finished: QSet() canceled: QSet() maybe-canceled: QSet() qt.widgets.gestures: QGestureManager:Recognizer: maybe gesture: QSwipeGesture(state=NoGesture,horizontalDirection=Right,verticalDirection=NoDirection,swipeAngle=0) QTouchEvent(TouchBegin device: "" states: Pressed, 1 points: QList(QEventPoint(id=0 ts=92554578 pos=429.5,265 scn=829.5,481 gbl=829.5,481 Pressed pressure=0.5 ellipse=(65.5x84.5 ? 0) vel=0,0 press=429.5,265 last=429.5,265 ? 0,0))) qt.widgets.gestures: QGestureManager::filterEventThroughContexts: activeGestures: QSet() maybeGestures: QSet(QSwipeGesture(state=NoGesture,horizontalDirection=Right,verticalDirection=NoDirection,swipeAngle=0)) started: QSet() triggered: QSet() finished: QSet() canceled: QSet() maybe-canceled: QSet() qt.widgets.gestures: QGestureManager:Recognizer: ignored the event: QSwipeGesture(state=NoGesture,horizontalDirection=Right,verticalDirection=NoDirection,swipeAngle=0) QTouchEvent(TouchUpdate device: "" states: Updated, 1 points: QList(QEventPoint(id=0 ts=92554578 pos=418,266 scn=818,482 gbl=818,482 Updated pressure=0.5 ellipse=(65.5x84.5 ? 0) vel=0,0 press=429.5,265 last=429.5,265 ? -11.5,1))) qt.widgets.gestures: QGestureManager:Recognizer: ignored the event: QSwipeGesture(state=NoGesture,horizontalDirection=Right,verticalDirection=NoDirection,swipeAngle=0) QTouchEvent(TouchUpdate device: "" states: Updated, 1 points: QList(QEventPoint(id=0 ts=92554578 pos=418,266 scn=818,482 gbl=818,482 Updated pressure=0.5 ellipse=(65.5x84.5 ? 0) vel=0,0 press=429.5,265 last=429.5,265 ? -11.5,1))) qt.widgets.gestures: QGestureManager:Recognizer: ignored the event: QSwipeGesture(state=NoGesture,horizontalDirection=Right,verticalDirection=NoDirection,swipeAngle=0) QTouchEvent(TouchUpdate device: "" states: Updated, 1 points: QList(QEventPoint(id=0 ts=92554578 pos=405,266.5 scn=805,482.5 gbl=805,482.5 Updated pressure=0.5 ellipse=(65.5x84 ? 0) vel=0,0 press=429.5,265 last=418,266 ? -13,0.5))) qt.widgets.gestures: QGestureManager:Recognizer: ignored the event: QSwipeGesture(state=NoGesture,horizontalDirection=Right,verticalDirection=NoDirection,swipeAngle=0) QTouchEvent(TouchUpdate device: "" states: Updated, 1 points: QList(QEventPoint(id=0 ts=92554578 pos=405,266.5 scn=805,482.5 gbl=805,482.5 Updated pressure=0.5 ellipse=(65.5x84 ? 0) vel=0,0 press=429.5,265 last=418,266 ? -13,0.5))) qt.widgets.gestures: QGestureManager:Recognizer: ignored the event: QSwipeGesture(state=NoGesture,horizontalDirection=Right,verticalDirection=NoDirection,swipeAngle=0) QMouseEvent(MouseMove pos=429.5,265 scn=429.5,265 gbl=829.5,481 dev=QInputDevice(QPointingDevice("" TouchScreen id=277 ptrType=Finger caps=Position|Area|NormalizedPosition|MouseEmulation buttonCount=1 maxPts=10 uniqueId=115)) source=MouseEventSynthesizedBySystem qt.widgets.gestures: QGestureManager:Recognizer: ignored the event: QSwipeGesture(state=NoGesture,horizontalDirection=Right,verticalDirection=NoDirection,swipeAngle=0) QMouseEvent(MouseMove pos=429.5,265 scn=429.5,265 gbl=829.5,481 dev=QInputDevice(QPointingDevice("" TouchScreen id=277 ptrType=Finger caps=Position|Area|NormalizedPosition|MouseEmulation buttonCount=1 maxPts=10 uniqueId=115)) source=MouseEventSynthesizedBySystem qt.widgets.gestures: QGestureManager:Recognizer: ignored the event: QSwipeGesture(state=NoGesture,horizontalDirection=Right,verticalDirection=NoDirection,swipeAngle=0) QMouseEvent(MouseButtonPress LeftButton pos=429.5,265 scn=429.5,265 gbl=829.5,481 dev=QInputDevice(QPointingDevice("" TouchScreen id=277 ptrType=Finger caps=Position|Area|NormalizedPosition|MouseEmulation buttonCount=1 maxPts=10 uniqueId=115)) source=MouseEventSynthesizedBySystem qt.widgets.gestures: QGestureManager:Recognizer: ignored the event: QSwipeGesture(state=NoGesture,horizontalDirection=Right,verticalDirection=NoDirection,swipeAngle=0) QMouseEvent(MouseButtonPress LeftButton pos=429.5,265 scn=429.5,265 gbl=829.5,481 dev=QInputDevice(QPointingDevice("" TouchScreen id=277 ptrType=Finger caps=Position|Area|NormalizedPosition|MouseEmulation buttonCount=1 maxPts=10 uniqueId=115)) source=MouseEventSynthesizedBySystem qt.widgets.gestures: QGestureManager:Recognizer: ignored the event: QSwipeGesture(state=NoGesture,horizontalDirection=Right,verticalDirection=NoDirection,swipeAngle=0) QMouseEvent(MouseMove btns=LeftButton pos=405,266.5 scn=405,266.5 gbl=805,482.5 dev=QInputDevice(QPointingDevice("" TouchScreen id=277 ptrType=Finger caps=Position|Area|NormalizedPosition|MouseEmulation buttonCount=1 maxPts=10 uniqueId=115)) source=MouseEventSynthesizedBySystem qt.widgets.gestures: QGestureManager:Recognizer: ignored the event: QSwipeGesture(state=NoGesture,horizontalDirection=Right,verticalDirection=NoDirection,swipeAngle=0) QMouseEvent(MouseMove btns=LeftButton pos=405,266.5 scn=405,266.5 gbl=805,482.5 dev=QInputDevice(QPointingDevice("" TouchScreen id=277 ptrType=Finger caps=Position|Area|NormalizedPosition|MouseEmulation buttonCount=1 maxPts=10 uniqueId=115)) source=MouseEventSynthesizedBySystem qt.widgets.gestures: QGestureManager:Recognizer: ignored the event: QSwipeGesture(state=NoGesture,horizontalDirection=Right,verticalDirection=NoDirection,swipeAngle=0) QTouchEvent(TouchUpdate device: "" states: Updated, 1 points: QList(QEventPoint(id=0 ts=92554593 pos=390,267 scn=790,483 gbl=790,483 Updated pressure=0.5 ellipse=(66x83.5 ? 0) vel=-700,23.3333 press=429.5,265 last=405,266.5 ? -15,0.5))) qt.widgets.gestures: QGestureManager:Recognizer: ignored the event: QSwipeGesture(state=NoGesture,horizontalDirection=Right,verticalDirection=NoDirection,swipeAngle=0) QTouchEvent(TouchUpdate device: "" states: Updated, 1 points: QList(QEventPoint(id=0 ts=92554593 pos=390,267 scn=790,483 gbl=790,483 Updated pressure=0.5 ellipse=(66x83.5 ? 0) vel=-700,23.3333 press=429.5,265 last=405,266.5 ? -15,0.5))) qt.widgets.gestures: QGestureManager:Recognizer: ignored the event: QSwipeGesture(state=NoGesture,horizontalDirection=Right,verticalDirection=NoDirection,swipeAngle=0) QMouseEvent(MouseMove btns=LeftButton pos=390,267 scn=390,267 gbl=790,483 dev=QInputDevice(QPointingDevice("" TouchScreen id=277 ptrType=Finger caps=Position|Area|NormalizedPosition|MouseEmulation buttonCount=1 maxPts=10 uniqueId=115)) source=MouseEventSynthesizedBySystem qt.widgets.gestures: QGestureManager:Recognizer: ignored the event: QSwipeGesture(state=NoGesture,horizontalDirection=Right,verticalDirection=NoDirection,swipeAngle=0) QMouseEvent(MouseMove btns=LeftButton pos=390,267 scn=390,267 gbl=790,483 dev=QInputDevice(QPointingDevice("" TouchScreen id=277 ptrType=Finger caps=Position|Area|NormalizedPosition|MouseEmulation buttonCount=1 maxPts=10 uniqueId=115)) source=MouseEventSynthesizedBySystem qt.widgets.gestures: QGestureManager:Recognizer: ignored the event: QSwipeGesture(state=NoGesture,horizontalDirection=Right,verticalDirection=NoDirection,swipeAngle=0) QTouchEvent(TouchUpdate device: "" states: Updated, 1 points: QList(QEventPoint(id=0 ts=92554593 pos=375.5,266 scn=775.5,482 gbl=775.5,482 Updated pressure=0.5 ellipse=(66x83 ? 0) vel=0,0 press=429.5,265 last=390,267 ? -14.5,-1))) qt.widgets.gestures: QGestureManager:Recognizer: ignored the event: QSwipeGesture(state=NoGesture,horizontalDirection=Right,verticalDirection=NoDirection,swipeAngle=0) QTouchEvent(TouchUpdate device: "" states: Updated, 1 points: QList(QEventPoint(id=0 ts=92554593 pos=375.5,266 scn=775.5,482 gbl=775.5,482 Updated pressure=0.5 ellipse=(66x83 ? 0) vel=0,0 press=429.5,265 last=390,267 ? -14.5,-1))) qt.widgets.gestures: QGestureManager:Recognizer: ignored the event: QSwipeGesture(state=NoGesture,horizontalDirection=Right,verticalDirection=NoDirection,swipeAngle=0) QMouseEvent(MouseMove btns=LeftButton pos=375.5,266 scn=375.5,266 gbl=775.5,482 dev=QInputDevice(QPointingDevice("" TouchScreen id=277 ptrType=Finger caps=Position|Area|NormalizedPosition|MouseEmulation buttonCount=1 maxPts=10 uniqueId=115)) source=MouseEventSynthesizedBySystem qt.widgets.gestures: QGestureManager:Recognizer: ignored the event: QSwipeGesture(state=NoGesture,horizontalDirection=Right,verticalDirection=NoDirection,swipeAngle=0) QMouseEvent(MouseMove btns=LeftButton pos=375.5,266 scn=375.5,266 gbl=775.5,482 dev=QInputDevice(QPointingDevice("" TouchScreen id=277 ptrType=Finger caps=Position|Area|NormalizedPosition|MouseEmulation buttonCount=1 maxPts=10 uniqueId=115)) source=MouseEventSynthesizedBySystem qt.widgets.gestures: QGestureManager:Recognizer: ignored the event: QSwipeGesture(state=NoGesture,horizontalDirection=Right,verticalDirection=NoDirection,swipeAngle=0) QTouchEvent(TouchUpdate device: "" states: Updated, 1 points: QList(QEventPoint(id=0 ts=92554609 pos=361.5,264 scn=761.5,480 gbl=761.5,480 Updated pressure=0.5 ellipse=(65x81 ? 0) vel=-612.5,-87.5 press=429.5,265 last=375.5,266 ? -14,-2))) qt.widgets.gestures: QGestureManager:Recognizer: ignored the event: QSwipeGesture(state=NoGesture,horizontalDirection=Right,verticalDirection=NoDirection,swipeAngle=0) QTouchEvent(TouchUpdate device: "" states: Updated, 1 points: QList(QEventPoint(id=0 ts=92554609 pos=361.5,264 scn=761.5,480 gbl=761.5,480 Updated pressure=0.5 ellipse=(65x81 ? 0) vel=-612.5,-87.5 press=429.5,265 last=375.5,266 ? -14,-2))) qt.widgets.gestures: QGestureManager:Recognizer: ignored the event: QSwipeGesture(state=NoGesture,horizontalDirection=Right,verticalDirection=NoDirection,swipeAngle=0) QMouseEvent(MouseMove btns=LeftButton pos=361.5,264 scn=361.5,264 gbl=761.5,480 dev=QInputDevice(QPointingDevice("" TouchScreen id=277 ptrType=Finger caps=Position|Area|NormalizedPosition|MouseEmulation buttonCount=1 maxPts=10 uniqueId=115)) source=MouseEventSynthesizedBySystem qt.widgets.gestures: QGestureManager:Recognizer: ignored the event: QSwipeGesture(state=NoGesture,horizontalDirection=Right,verticalDirection=NoDirection,swipeAngle=0) QMouseEvent(MouseMove btns=LeftButton pos=361.5,264 scn=361.5,264 gbl=761.5,480 dev=QInputDevice(QPointingDevice("" TouchScreen id=277 ptrType=Finger caps=Position|Area|NormalizedPosition|MouseEmulation buttonCount=1 maxPts=10 uniqueId=115)) source=MouseEventSynthesizedBySystem qt.widgets.gestures: QGestureManager:Recognizer: not gesture: QSwipeGesture(state=NoGesture,horizontalDirection=Right,verticalDirection=NoDirection,swipeAngle=0) QTouchEvent(TouchEnd device: "" states: Released, 1 points: QList(QEventPoint(id=0 ts=92554625 pos=361.5,264 scn=761.5,480 gbl=761.5,480 Released ellipse=(65x81 ? 0) vel=0,0 press=429.5,265 last=361.5,264 ? 0,0))) qt.widgets.gestures: QGestureManager:Recognizer: not gesture: QSwipeGesture(state=NoGesture,horizontalDirection=Right,verticalDirection=NoDirection,swipeAngle=0) QTouchEvent(TouchEnd device: "" states: Released, 1 points: QList(QEventPoint(id=0 ts=92554625 pos=361.5,264 scn=761.5,480 gbl=761.5,480 Released ellipse=(65x81 ? 0) vel=0,0 press=429.5,265 last=361.5,264 ? 0,0))) qt.widgets.gestures: QGestureManager:Recognizer: ignored the event: QSwipeGesture(state=NoGesture,horizontalDirection=Right,verticalDirection=NoDirection,swipeAngle=0) QMouseEvent(MouseButtonRelease LeftButton pos=361.5,264 scn=361.5,264 gbl=761.5,480 dev=QInputDevice(QPointingDevice("" TouchScreen id=277 ptrType=Finger caps=Position|Area|NormalizedPosition|MouseEmulation buttonCount=1 maxPts=10 uniqueId=115)) source=MouseEventSynthesizedBySystem qt.widgets.gestures: QGestureManager:Recognizer: ignored the event: QSwipeGesture(state=NoGesture,horizontalDirection=Right,verticalDirection=NoDirection,swipeAngle=0) QMouseEvent(MouseButtonRelease LeftButton pos=361.5,264 scn=361.5,264 gbl=761.5,480 dev=QInputDevice(QPointingDevice("" TouchScreen id=277 ptrType=Finger caps=Position|Area|NormalizedPosition|MouseEmulation buttonCount=1 maxPts=10 uniqueId=115)) source=MouseEventSynthesizedBySystemAs per the end of each statement, I suspect touches are being turned into mouse events (the end of each line says 'source=MouseEventSynthesizedBySystem')
Not sure why its doing that... or if there's a way to turn it off
According to https://doc.qt.io/qtforpython-6/PySide6/QtGui/QTouchEvent.html:
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.
However, adding:
w = Widget()
w.setAttribute(Qt.WA_AcceptTouchEvents, True)Appears to have no effect...
Any thoughts?
-
Hi
Ok - While the test program generates loads of output, unfortunately its still not recognising a swipe... I did note that your example is using PyQT6, I hadn't mentioned that I'm using PySide6 - however, I believe that they should be compatible.As I'm only interested in swipes, I removed the other gesture types from the list, so I peared down the program to:
from PySide6.QtWidgets import QApplication, QWidget from PySide6.QtCore import QEvent, QLoggingCategory, Qt class Widget(QWidget): def event(self, e: QEvent) -> bool: if QEvent.Type.Gesture == e.type(): print(f"Gesture delivered: {e.gestures()}") return super().event(e) app = QApplication([]) QLoggingCategory.setFilterRules("qt.widgets.gestures=true") w = Widget() w.grabGesture(Qt.GestureType.SwipeGesture) w.show() app.exec()Meanwhile - I opened the program, did a very quick swipe right to left, and here's the output:
qt.widgets.gestures: QGestureManager:Recognizer: maybe gesture: QSwipeGesture(state=NoGesture,horizontalDirection=Right,verticalDirection=NoDirection,swipeAngle=0) QTouchEvent(TouchBegin device: "" states: Pressed, 1 points: QList(QEventPoint(id=0 ts=92554578 pos=429.5,265 scn=829.5,481 gbl=829.5,481 Pressed pressure=0.5 ellipse=(65.5x84.5 ? 0) vel=0,0 press=429.5,265 last=429.5,265 ? 0,0))) qt.widgets.gestures: QGestureManager::filterEventThroughContexts: activeGestures: QSet() maybeGestures: QSet(QSwipeGesture(state=NoGesture,horizontalDirection=Right,verticalDirection=NoDirection,swipeAngle=0)) started: QSet() triggered: QSet() finished: QSet() canceled: QSet() maybe-canceled: QSet() qt.widgets.gestures: QGestureManager:Recognizer: maybe gesture: QSwipeGesture(state=NoGesture,horizontalDirection=Right,verticalDirection=NoDirection,swipeAngle=0) QTouchEvent(TouchBegin device: "" states: Pressed, 1 points: QList(QEventPoint(id=0 ts=92554578 pos=429.5,265 scn=829.5,481 gbl=829.5,481 Pressed pressure=0.5 ellipse=(65.5x84.5 ? 0) vel=0,0 press=429.5,265 last=429.5,265 ? 0,0))) qt.widgets.gestures: QGestureManager::filterEventThroughContexts: activeGestures: QSet() maybeGestures: QSet(QSwipeGesture(state=NoGesture,horizontalDirection=Right,verticalDirection=NoDirection,swipeAngle=0)) started: QSet() triggered: QSet() finished: QSet() canceled: QSet() maybe-canceled: QSet() qt.widgets.gestures: QGestureManager:Recognizer: ignored the event: QSwipeGesture(state=NoGesture,horizontalDirection=Right,verticalDirection=NoDirection,swipeAngle=0) QTouchEvent(TouchUpdate device: "" states: Updated, 1 points: QList(QEventPoint(id=0 ts=92554578 pos=418,266 scn=818,482 gbl=818,482 Updated pressure=0.5 ellipse=(65.5x84.5 ? 0) vel=0,0 press=429.5,265 last=429.5,265 ? -11.5,1))) qt.widgets.gestures: QGestureManager:Recognizer: ignored the event: QSwipeGesture(state=NoGesture,horizontalDirection=Right,verticalDirection=NoDirection,swipeAngle=0) QTouchEvent(TouchUpdate device: "" states: Updated, 1 points: QList(QEventPoint(id=0 ts=92554578 pos=418,266 scn=818,482 gbl=818,482 Updated pressure=0.5 ellipse=(65.5x84.5 ? 0) vel=0,0 press=429.5,265 last=429.5,265 ? -11.5,1))) qt.widgets.gestures: QGestureManager:Recognizer: ignored the event: QSwipeGesture(state=NoGesture,horizontalDirection=Right,verticalDirection=NoDirection,swipeAngle=0) QTouchEvent(TouchUpdate device: "" states: Updated, 1 points: QList(QEventPoint(id=0 ts=92554578 pos=405,266.5 scn=805,482.5 gbl=805,482.5 Updated pressure=0.5 ellipse=(65.5x84 ? 0) vel=0,0 press=429.5,265 last=418,266 ? -13,0.5))) qt.widgets.gestures: QGestureManager:Recognizer: ignored the event: QSwipeGesture(state=NoGesture,horizontalDirection=Right,verticalDirection=NoDirection,swipeAngle=0) QTouchEvent(TouchUpdate device: "" states: Updated, 1 points: QList(QEventPoint(id=0 ts=92554578 pos=405,266.5 scn=805,482.5 gbl=805,482.5 Updated pressure=0.5 ellipse=(65.5x84 ? 0) vel=0,0 press=429.5,265 last=418,266 ? -13,0.5))) qt.widgets.gestures: QGestureManager:Recognizer: ignored the event: QSwipeGesture(state=NoGesture,horizontalDirection=Right,verticalDirection=NoDirection,swipeAngle=0) QMouseEvent(MouseMove pos=429.5,265 scn=429.5,265 gbl=829.5,481 dev=QInputDevice(QPointingDevice("" TouchScreen id=277 ptrType=Finger caps=Position|Area|NormalizedPosition|MouseEmulation buttonCount=1 maxPts=10 uniqueId=115)) source=MouseEventSynthesizedBySystem qt.widgets.gestures: QGestureManager:Recognizer: ignored the event: QSwipeGesture(state=NoGesture,horizontalDirection=Right,verticalDirection=NoDirection,swipeAngle=0) QMouseEvent(MouseMove pos=429.5,265 scn=429.5,265 gbl=829.5,481 dev=QInputDevice(QPointingDevice("" TouchScreen id=277 ptrType=Finger caps=Position|Area|NormalizedPosition|MouseEmulation buttonCount=1 maxPts=10 uniqueId=115)) source=MouseEventSynthesizedBySystem qt.widgets.gestures: QGestureManager:Recognizer: ignored the event: QSwipeGesture(state=NoGesture,horizontalDirection=Right,verticalDirection=NoDirection,swipeAngle=0) QMouseEvent(MouseButtonPress LeftButton pos=429.5,265 scn=429.5,265 gbl=829.5,481 dev=QInputDevice(QPointingDevice("" TouchScreen id=277 ptrType=Finger caps=Position|Area|NormalizedPosition|MouseEmulation buttonCount=1 maxPts=10 uniqueId=115)) source=MouseEventSynthesizedBySystem qt.widgets.gestures: QGestureManager:Recognizer: ignored the event: QSwipeGesture(state=NoGesture,horizontalDirection=Right,verticalDirection=NoDirection,swipeAngle=0) QMouseEvent(MouseButtonPress LeftButton pos=429.5,265 scn=429.5,265 gbl=829.5,481 dev=QInputDevice(QPointingDevice("" TouchScreen id=277 ptrType=Finger caps=Position|Area|NormalizedPosition|MouseEmulation buttonCount=1 maxPts=10 uniqueId=115)) source=MouseEventSynthesizedBySystem qt.widgets.gestures: QGestureManager:Recognizer: ignored the event: QSwipeGesture(state=NoGesture,horizontalDirection=Right,verticalDirection=NoDirection,swipeAngle=0) QMouseEvent(MouseMove btns=LeftButton pos=405,266.5 scn=405,266.5 gbl=805,482.5 dev=QInputDevice(QPointingDevice("" TouchScreen id=277 ptrType=Finger caps=Position|Area|NormalizedPosition|MouseEmulation buttonCount=1 maxPts=10 uniqueId=115)) source=MouseEventSynthesizedBySystem qt.widgets.gestures: QGestureManager:Recognizer: ignored the event: QSwipeGesture(state=NoGesture,horizontalDirection=Right,verticalDirection=NoDirection,swipeAngle=0) QMouseEvent(MouseMove btns=LeftButton pos=405,266.5 scn=405,266.5 gbl=805,482.5 dev=QInputDevice(QPointingDevice("" TouchScreen id=277 ptrType=Finger caps=Position|Area|NormalizedPosition|MouseEmulation buttonCount=1 maxPts=10 uniqueId=115)) source=MouseEventSynthesizedBySystem qt.widgets.gestures: QGestureManager:Recognizer: ignored the event: QSwipeGesture(state=NoGesture,horizontalDirection=Right,verticalDirection=NoDirection,swipeAngle=0) QTouchEvent(TouchUpdate device: "" states: Updated, 1 points: QList(QEventPoint(id=0 ts=92554593 pos=390,267 scn=790,483 gbl=790,483 Updated pressure=0.5 ellipse=(66x83.5 ? 0) vel=-700,23.3333 press=429.5,265 last=405,266.5 ? -15,0.5))) qt.widgets.gestures: QGestureManager:Recognizer: ignored the event: QSwipeGesture(state=NoGesture,horizontalDirection=Right,verticalDirection=NoDirection,swipeAngle=0) QTouchEvent(TouchUpdate device: "" states: Updated, 1 points: QList(QEventPoint(id=0 ts=92554593 pos=390,267 scn=790,483 gbl=790,483 Updated pressure=0.5 ellipse=(66x83.5 ? 0) vel=-700,23.3333 press=429.5,265 last=405,266.5 ? -15,0.5))) qt.widgets.gestures: QGestureManager:Recognizer: ignored the event: QSwipeGesture(state=NoGesture,horizontalDirection=Right,verticalDirection=NoDirection,swipeAngle=0) QMouseEvent(MouseMove btns=LeftButton pos=390,267 scn=390,267 gbl=790,483 dev=QInputDevice(QPointingDevice("" TouchScreen id=277 ptrType=Finger caps=Position|Area|NormalizedPosition|MouseEmulation buttonCount=1 maxPts=10 uniqueId=115)) source=MouseEventSynthesizedBySystem qt.widgets.gestures: QGestureManager:Recognizer: ignored the event: QSwipeGesture(state=NoGesture,horizontalDirection=Right,verticalDirection=NoDirection,swipeAngle=0) QMouseEvent(MouseMove btns=LeftButton pos=390,267 scn=390,267 gbl=790,483 dev=QInputDevice(QPointingDevice("" TouchScreen id=277 ptrType=Finger caps=Position|Area|NormalizedPosition|MouseEmulation buttonCount=1 maxPts=10 uniqueId=115)) source=MouseEventSynthesizedBySystem qt.widgets.gestures: QGestureManager:Recognizer: ignored the event: QSwipeGesture(state=NoGesture,horizontalDirection=Right,verticalDirection=NoDirection,swipeAngle=0) QTouchEvent(TouchUpdate device: "" states: Updated, 1 points: QList(QEventPoint(id=0 ts=92554593 pos=375.5,266 scn=775.5,482 gbl=775.5,482 Updated pressure=0.5 ellipse=(66x83 ? 0) vel=0,0 press=429.5,265 last=390,267 ? -14.5,-1))) qt.widgets.gestures: QGestureManager:Recognizer: ignored the event: QSwipeGesture(state=NoGesture,horizontalDirection=Right,verticalDirection=NoDirection,swipeAngle=0) QTouchEvent(TouchUpdate device: "" states: Updated, 1 points: QList(QEventPoint(id=0 ts=92554593 pos=375.5,266 scn=775.5,482 gbl=775.5,482 Updated pressure=0.5 ellipse=(66x83 ? 0) vel=0,0 press=429.5,265 last=390,267 ? -14.5,-1))) qt.widgets.gestures: QGestureManager:Recognizer: ignored the event: QSwipeGesture(state=NoGesture,horizontalDirection=Right,verticalDirection=NoDirection,swipeAngle=0) QMouseEvent(MouseMove btns=LeftButton pos=375.5,266 scn=375.5,266 gbl=775.5,482 dev=QInputDevice(QPointingDevice("" TouchScreen id=277 ptrType=Finger caps=Position|Area|NormalizedPosition|MouseEmulation buttonCount=1 maxPts=10 uniqueId=115)) source=MouseEventSynthesizedBySystem qt.widgets.gestures: QGestureManager:Recognizer: ignored the event: QSwipeGesture(state=NoGesture,horizontalDirection=Right,verticalDirection=NoDirection,swipeAngle=0) QMouseEvent(MouseMove btns=LeftButton pos=375.5,266 scn=375.5,266 gbl=775.5,482 dev=QInputDevice(QPointingDevice("" TouchScreen id=277 ptrType=Finger caps=Position|Area|NormalizedPosition|MouseEmulation buttonCount=1 maxPts=10 uniqueId=115)) source=MouseEventSynthesizedBySystem qt.widgets.gestures: QGestureManager:Recognizer: ignored the event: QSwipeGesture(state=NoGesture,horizontalDirection=Right,verticalDirection=NoDirection,swipeAngle=0) QTouchEvent(TouchUpdate device: "" states: Updated, 1 points: QList(QEventPoint(id=0 ts=92554609 pos=361.5,264 scn=761.5,480 gbl=761.5,480 Updated pressure=0.5 ellipse=(65x81 ? 0) vel=-612.5,-87.5 press=429.5,265 last=375.5,266 ? -14,-2))) qt.widgets.gestures: QGestureManager:Recognizer: ignored the event: QSwipeGesture(state=NoGesture,horizontalDirection=Right,verticalDirection=NoDirection,swipeAngle=0) QTouchEvent(TouchUpdate device: "" states: Updated, 1 points: QList(QEventPoint(id=0 ts=92554609 pos=361.5,264 scn=761.5,480 gbl=761.5,480 Updated pressure=0.5 ellipse=(65x81 ? 0) vel=-612.5,-87.5 press=429.5,265 last=375.5,266 ? -14,-2))) qt.widgets.gestures: QGestureManager:Recognizer: ignored the event: QSwipeGesture(state=NoGesture,horizontalDirection=Right,verticalDirection=NoDirection,swipeAngle=0) QMouseEvent(MouseMove btns=LeftButton pos=361.5,264 scn=361.5,264 gbl=761.5,480 dev=QInputDevice(QPointingDevice("" TouchScreen id=277 ptrType=Finger caps=Position|Area|NormalizedPosition|MouseEmulation buttonCount=1 maxPts=10 uniqueId=115)) source=MouseEventSynthesizedBySystem qt.widgets.gestures: QGestureManager:Recognizer: ignored the event: QSwipeGesture(state=NoGesture,horizontalDirection=Right,verticalDirection=NoDirection,swipeAngle=0) QMouseEvent(MouseMove btns=LeftButton pos=361.5,264 scn=361.5,264 gbl=761.5,480 dev=QInputDevice(QPointingDevice("" TouchScreen id=277 ptrType=Finger caps=Position|Area|NormalizedPosition|MouseEmulation buttonCount=1 maxPts=10 uniqueId=115)) source=MouseEventSynthesizedBySystem qt.widgets.gestures: QGestureManager:Recognizer: not gesture: QSwipeGesture(state=NoGesture,horizontalDirection=Right,verticalDirection=NoDirection,swipeAngle=0) QTouchEvent(TouchEnd device: "" states: Released, 1 points: QList(QEventPoint(id=0 ts=92554625 pos=361.5,264 scn=761.5,480 gbl=761.5,480 Released ellipse=(65x81 ? 0) vel=0,0 press=429.5,265 last=361.5,264 ? 0,0))) qt.widgets.gestures: QGestureManager:Recognizer: not gesture: QSwipeGesture(state=NoGesture,horizontalDirection=Right,verticalDirection=NoDirection,swipeAngle=0) QTouchEvent(TouchEnd device: "" states: Released, 1 points: QList(QEventPoint(id=0 ts=92554625 pos=361.5,264 scn=761.5,480 gbl=761.5,480 Released ellipse=(65x81 ? 0) vel=0,0 press=429.5,265 last=361.5,264 ? 0,0))) qt.widgets.gestures: QGestureManager:Recognizer: ignored the event: QSwipeGesture(state=NoGesture,horizontalDirection=Right,verticalDirection=NoDirection,swipeAngle=0) QMouseEvent(MouseButtonRelease LeftButton pos=361.5,264 scn=361.5,264 gbl=761.5,480 dev=QInputDevice(QPointingDevice("" TouchScreen id=277 ptrType=Finger caps=Position|Area|NormalizedPosition|MouseEmulation buttonCount=1 maxPts=10 uniqueId=115)) source=MouseEventSynthesizedBySystem qt.widgets.gestures: QGestureManager:Recognizer: ignored the event: QSwipeGesture(state=NoGesture,horizontalDirection=Right,verticalDirection=NoDirection,swipeAngle=0) QMouseEvent(MouseButtonRelease LeftButton pos=361.5,264 scn=361.5,264 gbl=761.5,480 dev=QInputDevice(QPointingDevice("" TouchScreen id=277 ptrType=Finger caps=Position|Area|NormalizedPosition|MouseEmulation buttonCount=1 maxPts=10 uniqueId=115)) source=MouseEventSynthesizedBySystemAs per the end of each statement, I suspect touches are being turned into mouse events (the end of each line says 'source=MouseEventSynthesizedBySystem')
Not sure why its doing that... or if there's a way to turn it off
According to https://doc.qt.io/qtforpython-6/PySide6/QtGui/QTouchEvent.html:
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.
However, adding:
w = Widget()
w.setAttribute(Qt.WA_AcceptTouchEvents, True)Appears to have no effect...
Any thoughts?
@CarlB said in Python / PySide6 - swipe gesture?:
Hi
Ok - While the test program generates loads of output, unfortunately its still not recognising a swipe... I did note that your example is using PyQT6, I hadn't mentioned that I'm using PySide6 - however, I believe that they should be compatible.I don't expect a difference either. The first version was written in C++, and no PyQt-specific changes were required to port it.
Meanwhile - I opened the program, did a very quick swipe right to left, and here's the output:
qt.widgets.gestures: QGestureManager:Recognizer: maybe gesture: QSwipeGesture(state=NoGesture,horizontalDirection=Right,verticalDirection=NoDirection,swipeAngle=0) QTouchEvent(TouchBegin device: "" states: Pressed, 1 points: QList(QEventPoint(id=0 ts=92554578 pos=429.5,265 scn=829.5,481 gbl=829.5,481 Pressed pressure=0.5 ellipse=(65.5x84.5 ? 0) vel=0,0 press=429.5,265 last=429.5,265 ? 0,0))) qt.widgets.gestures: QGestureManager::filterEventThroughContexts: activeGestures: QSet() maybeGestures: QSet(QSwipeGesture(state=NoGesture,horizontalDirection=Right,verticalDirection=NoDirection,swipeAngle=0)) started: QSet() triggered: QSet() finished: QSet() canceled: QSet() maybe-canceled: QSet() qt.widgets.gestures: QGestureManager:Recognizer: maybe gesture: QSwipeGesture(state=NoGesture,horizontalDirection=Right,verticalDirection=NoDirection,swipeAngle=0) QTouchEvent(TouchBegin device: "" states: Pressed, 1 points: QList(QEventPoint(id=0 ts=92554578 pos=429.5,265 scn=829.5,481 gbl=829.5,481 Pressed pressure=0.5 ellipse=(65.5x84.5 ? 0) vel=0,0 press=429.5,265 last=429.5,265 ? 0,0))) qt.widgets.gestures: QGestureManager::filterEventThroughContexts: activeGestures: QSet() maybeGestures: QSet(QSwipeGesture(state=NoGesture,horizontalDirection=Right,verticalDirection=NoDirection,swipeAngle=0)) started: QSet() triggered: QSet() finished: QSet() canceled: QSet() maybe-canceled: QSet() qt.widgets.gestures: QGestureManager:Recognizer: ignored the event: QSwipeGesture(state=NoGesture,horizontalDirection=Right,verticalDirection=NoDirection,swipeAngle=0) QTouchEvent(TouchUpdate device: "" states: Updated, 1 points: QList(QEventPoint(id=0 ts=92554578 pos=418,266 scn=818,482 gbl=818,482 Updated pressure=0.5 ellipse=(65.5x84.5 ? 0) vel=0,0 press=429.5,265 last=429.5,265 ? -11.5,1)))The swipe gesture recognizer starts out by indicating that the first two events might be related to a swipe, so we know that the right type of input is being received. Changing touch input settings is apparently not warranted. It then switches to ignoring the events, before eventually declaring "not gesture". Looking at QSwipeGestureRecognizer::recognize , there's a state change that occurs when 3 touch points are present.
else if (ev->points().size() == 3) { d->state = QSwipeGesturePrivate::ThreePointsReached;The attempted swipe in the logs above appears to be limited to a single touch point. Does a 3 finger swipe generate substantially different output?