I was trying to handle QTabletEvent events generated from a WACOM Intuos ART tablet.
I succeeded with the following code but was obliged to resize my QWidget handling the tablet events to the full Windows desktop size in order to receive the tablet events from all its surface (whereever the pen is located). It seems by default the tablet surface is mapped to the full desktop. I want to map it to my own widget client area.
How to do this ?
import sys
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
class TabletSampleWindow(QWidget):
def __init__(self, parent=None):
super(TabletSampleWindow, self).__init__(parent)
self.pen_is_down = False
self.pen_x = 0
self.pen_y = 0
self.pen_pressure = 0
self.text = ""
# Resizing the sample window to full desktop size:
frame_rect = app.desktop().frameGeometry()
width, height = frame_rect.width(), frame_rect.height()
self.resize(width, height)
self.move(-9, 0)
self.setWindowTitle("Sample Tablet Event Handling")
def tabletEvent(self, tabletEvent):
self.pen_x = tabletEvent.globalX()
self.pen_y = tabletEvent.globalY()
self.pen_pressure = int(tabletEvent.pressure() * 100)
if tabletEvent.type() == QTabletEvent.TabletPress:
self.pen_is_down = True
self.text = "TabletPress event"
elif tabletEvent.type() == QTabletEvent.TabletMove:
self.pen_is_down = True
self.text = "TabletMove event"
elif tabletEvent.type() == QTabletEvent.TabletRelease:
self.pen_is_down = False
self.text = "TabletRelease event"
self.text += " at x={0}, y={1}, pressure={2}%,".format(self.pen_x, self.pen_y, self.pen_pressure)
if self.pen_is_down:
self.text += " Pen is down."
else:
self.text += " Pen is up."
tabletEvent.accept()
self.update()
def paintEvent(self, event):
text = self.text
i = text.find("\n\n")
if i >= 0:
text = text.left(i)
painter = QPainter(self)
painter.setRenderHint(QPainter.TextAntialiasing)
painter.drawText(self.rect(), Qt.AlignTop | Qt.AlignLeft , text)
app = QApplication(sys.argv)
mainform = TabletSampleWindow()
mainform.show()
app.exec_()