Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save stevenliebregt/8e4211937b671ac637b610650a11914f to your computer and use it in GitHub Desktop.
Save stevenliebregt/8e4211937b671ac637b610650a11914f to your computer and use it in GitHub Desktop.
PyQt5 Example of the eventFilter, and a TouchEvent capture.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys
from PyQt5.QtCore import QEvent
from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import QApplication
from PyQt5.QtWidgets import QMainWindow
class MyMainWindow(QMainWindow):
"""
A subclass of the QMainWindow with the eventFilter method.
"""
def __init__(self, parent=None):
super(MyMainWindow, self).__init__(parent)
def eventFilter(self, obj, event):
if event.type() == QEvent.TouchBegin: # Catch the TouchBegin event.
print('We have a touch begin')
return True
elif event.type() == QEvent.TouchEnd: # Catch the TouchEnd event.
print('We have a touch end')
return True
return super(MyMainWindow, self).eventFilter(obj, event)
def main():
app = QApplication(sys.argv)
# Create a window.
win = MyMainWindow()
win.resize(800, 800)
win.setWindowTitle('Touch Event Example')
# Tell the window that we accept touch events.
win.setAttribute(Qt.WA_AcceptTouchEvents, True)
# Install an event filter to filter the touch events.
win.installEventFilter(win)
# Show the window and execute the app.
win.show()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
@ahlixinjie
Copy link

thx

@andybaran
Copy link

Thanks! This is great to use as a starting point for QT touch interactions!! :)

@JeonHyeongJunKW
Copy link

thanks

@leofox2021
Copy link

Thank you for this, can you do the same with L/R mouse button clicks?

@SimonSAMPERE
Copy link

thanks a lot @stevenliebregt, you can't imagine how helpful this is

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment