Created
June 15, 2018 14:52
-
-
Save stevenliebregt/8e4211937b671ac637b610650a11914f to your computer and use it in GitHub Desktop.
PyQt5 Example of the eventFilter, and a TouchEvent capture.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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() |
Thanks! This is great to use as a starting point for QT touch interactions!! :)
thanks
Thank you for this, can you do the same with L/R mouse button clicks?
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
thx