However, such predefined methods do not contain anything specific for gestures produced by a trackpad in MacOS.
This article tells you how a gesture event can be obtained and handled in PyQt or PySide2.
Where a Gesture Event is Generated?
As documented in Qt, a gesture event is stored as a
However, the native
QMainWindow classes do not include methods for handling such an event directly.
As a result, we will have to recognize the gesture events by ourselves in the
Overriding the event() Method
QMainWindow class, use the following method as a base to override the
def event(self, e): pass return QMainWindow(self, e)where
passis where we are going to recognize our gesture events (described next) and
QMainWindowshould be altered by
QWidgetif you are using
Handling Gesture Events
QNativeGestureEvent, remember to import the dependency:
from PySide2.QtGui import QNativeGestureEventin which
PyQtif that’s your case.
Now we can check the type for the incoming event:
def event(self, e): if isinstance(e, QNativeGestureEvent): print(e.gestureType(), e.pos(), e.value()) return QMainWindow(self, e)in which we print three most important values when a gesture event is identified:
e.gestureType()gives you the type of gestures (either pinch, zoom or rotate) the current event is.
e.pos()gives you the cursor position where gesture is happening.
e.value()gives you the percentage of either the degree of zooming or rotating angle.
For detailed description, check the Qt official page.
Note that doing what stated above does not impact how you handle events from other overridden event handling methods since we have called
return QMainWindow(self, e).