Guest User

PyQt5 program demonstrating a bug with tablet events

a guest
May 10th, 2020
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.55 KB | None | 0 0
  1. import sys
  2.  
  3. from PyQt5 import QtCore
  4. from PyQt5 import QtGui
  5. from PyQt5 import QtWidgets
  6.  
  7.  
  8. class TouchWidget(QtWidgets.QWidget):
  9.  
  10.   escPressed = QtCore.pyqtSignal()
  11.  
  12.   def __init__(self, parent=None):
  13.     super(TouchWidget, self).__init__(parent)
  14.     self.setAttribute(QtCore.Qt.WA_AcceptTouchEvents)
  15.  
  16.   def keyPressEvent(self, event):
  17.     if event.key() == QtCore.Qt.Key_Escape:
  18.       self.escPressed.emit()
  19.     return super(TouchWidget, self).keyPressEvent(event)
  20.  
  21.   def event(self, event):
  22.     if event.type() == QtCore.QEvent.TouchUpdate:
  23.       self.onTouchUpdate(event)
  24.     return super(TouchWidget, self).event(event)
  25.  
  26.   def onTouchUpdate(self, event: QtGui.QTouchEvent):
  27.     tps = event.touchPoints()
  28.     released = False
  29.     for i, tp in enumerate(tps):
  30.       if i:
  31.         print("  ", end="")
  32.       print("p%d: %.2f/%.2f" % (i, tp.pos().x(), tp.pos().y()), end="")
  33.       if tp.state() == QtCore.Qt.TouchPointPressed:
  34.         print("    (PRESSED)", end="")
  35.       elif tp.state() == QtCore.Qt.TouchPointMoved:
  36.         print("      (MOVED)", end="")
  37.       elif tp.state() == QtCore.Qt.TouchPointStationary:
  38.         print(" (STATIONARY)", end="")
  39.       elif tp.state() == QtCore.Qt.TouchPointReleased:
  40.         print("   (RELEASED)", end="")
  41.         released = True
  42.     print()
  43.     if released:
  44.       print()
  45.       print()
  46.       print()
  47.  
  48.  
  49. if __name__ == "__main__":
  50.   print("""
  51.  This program demonstrates a bug in Qt5's handling of touch gestures.
  52.  
  53.  A touch gesture event is made of touchpoints. Every time that event fires,
  54.  the touchpoints that it carries can have one of four states: PRESSED,
  55.  RELEASED, STATIONARY, or MOVED. Those touchpoints also contain the
  56.  coordinates at which the touch occurred.
  57.  
  58.  In order to use this program, run it, move the mouse pointer to the new
  59.  window, and perform a two-finder pinch/rotate gesture.
  60.  
  61.  The output of this program shows that touchpoints report spurious coordinates
  62.  while in state PRESSED, and tend to keep doing so until the touchpoint gets
  63.  to state MOVED for the first time. A TouchPoint in state RELEASED also tends
  64.  to "jump" some distance from its last known position.
  65.  
  66.  This is with the TouchPoint.pos() method. When using
  67.  TouchPoint.normalizedPos() instead, the coordinates are correct, expect when
  68.  the TouchPoint is in state RELEASED, where the same "jump" as above can be
  69.  observed.
  70.  
  71.  Quit this program by pressing Escape.
  72.  
  73.  """)
  74.   app = QtWidgets.QApplication(sys.argv)
  75.   w = TouchWidget()
  76.   w.show()
  77.   w.escPressed.connect(app.quit)
  78.   app.exec_()
Add Comment
Please, Sign In to add comment