Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import sys
- from PyQt5 import QtCore
- from PyQt5 import QtGui
- from PyQt5 import QtWidgets
- class TouchWidget(QtWidgets.QWidget):
- escPressed = QtCore.pyqtSignal()
- def __init__(self, parent=None):
- super(TouchWidget, self).__init__(parent)
- self.setAttribute(QtCore.Qt.WA_AcceptTouchEvents)
- def keyPressEvent(self, event):
- if event.key() == QtCore.Qt.Key_Escape:
- self.escPressed.emit()
- return super(TouchWidget, self).keyPressEvent(event)
- def event(self, event):
- if event.type() == QtCore.QEvent.TouchUpdate:
- self.onTouchUpdate(event)
- return super(TouchWidget, self).event(event)
- def onTouchUpdate(self, event: QtGui.QTouchEvent):
- tps = event.touchPoints()
- released = False
- for i, tp in enumerate(tps):
- if i:
- print(" ", end="")
- print("p%d: %.2f/%.2f" % (i, tp.pos().x(), tp.pos().y()), end="")
- if tp.state() == QtCore.Qt.TouchPointPressed:
- print(" (PRESSED)", end="")
- elif tp.state() == QtCore.Qt.TouchPointMoved:
- print(" (MOVED)", end="")
- elif tp.state() == QtCore.Qt.TouchPointStationary:
- print(" (STATIONARY)", end="")
- elif tp.state() == QtCore.Qt.TouchPointReleased:
- print(" (RELEASED)", end="")
- released = True
- print()
- if released:
- print()
- print()
- print()
- if __name__ == "__main__":
- print("""
- This program demonstrates a bug in Qt5's handling of touch gestures.
- A touch gesture event is made of touchpoints. Every time that event fires,
- the touchpoints that it carries can have one of four states: PRESSED,
- RELEASED, STATIONARY, or MOVED. Those touchpoints also contain the
- coordinates at which the touch occurred.
- In order to use this program, run it, move the mouse pointer to the new
- window, and perform a two-finder pinch/rotate gesture.
- The output of this program shows that touchpoints report spurious coordinates
- while in state PRESSED, and tend to keep doing so until the touchpoint gets
- to state MOVED for the first time. A TouchPoint in state RELEASED also tends
- to "jump" some distance from its last known position.
- This is with the TouchPoint.pos() method. When using
- TouchPoint.normalizedPos() instead, the coordinates are correct, expect when
- the TouchPoint is in state RELEASED, where the same "jump" as above can be
- observed.
- Quit this program by pressing Escape.
- """)
- app = QtWidgets.QApplication(sys.argv)
- w = TouchWidget()
- w.show()
- w.escPressed.connect(app.quit)
- app.exec_()
Add Comment
Please, Sign In to add comment