Handling stylus input in QML
-
Hi,
I'm trying to handle the input of a stylus on a tablet in QML, but I'm stuck.
Qt has an example on how to use classic C++ to draw on a tablet. In the example they inherit QWidget to create a canvas which can handle QTabletEvent-events, allowing you to draw thick and thin lines based on the pen preassure. It works fine on my tablet.
I'm trying to get the same in modern QML. From what I have found, the QML element to handle this would be PointHandler. I can't get it to handle pen input, though. This is my last code to test this:
Window { visible: true width: 640 height: 480 id: main title: qsTr("Hello World") Label { id: label anchors.centerIn: parent } Rectangle { anchors.left: main.left anchors.top: main.top width: main.width height: main.height opacity: 0 PointHandler { acceptedDevices: PointerDevice.AllDevices onActiveChanged: { label.text = (active == true ) ? "AllDevices" : "" } } PointHandler { acceptedDevices: PointerDevice.UnknownDevice onActiveChanged: { label.text = (active == true ) ? "UnknownDevice" : "" } } PointHandler { acceptedDevices: PointerDevice.Mouse onActiveChanged: { label.text = (active == true ) ? "Mouse " + point.pressure : "" } } PointHandler { acceptedDevices: PointerDevice.TouchScreen onActiveChanged: { label.text = (active == true ) ? "TouchScreen" : "" } } PointHandler { acceptedDevices: PointerDevice.TouchPad onActiveChanged: { label.text = (active == true ) ? "TouchPad" : "" } } PointHandler { acceptedDevices: PointerDevice.Stylus onActiveChanged: { label.text = (active == true ) ? "Stylus" : "" } } PointHandler { acceptedDevices: PointerDevice.Airbrush onActiveChanged: { label.text = (active == true ) ? "Airbrush" : "" } } PointHandler { acceptedDevices: PointerDevice.Puck onActiveChanged: { label.text = (active == true ) ? "Puck" : "" } } } }
Since it didn't work I added a PointHandler for each type to see what happens. Turns out, the stylus triggers the PointHandler for Mouse instead of Stylus. So far I wasn't able to find out how to change this. I hope someone here can give me some pointers on that.
Regards
Thorsten
-
-
After some try-and-error I found out that QGuiApplication recieves QTabletEvents, but they are not sent to the QQmlApplicationEngine.
At least installing an eventfilter that sends a message to qDebug when QTabletEvents are encountered reacts this way.