QToolButton Remain Pressed after finger has been removed from Touchscreen

530 Views Asked by At

I have a small Qt Desktop application (in C++) which I need to make it work in a touch screen device running Window 10. The device has a touchscreen and application works perfectly with keyboard and mouse.

I am not expert in developing Qt Application and that's why unable to resolve this could be silly isssue.

However, when I try to use touchscreen, the last touched QToolbutton remain pressed even if I have moved my finger away from the touchscreen and when I touch somewhere else, then that QToolButton is released.

I expect the Qtoolbutton to behave just like when it is pressed using a mouse. Once I move my finger off the touchscreen, it should be released.

I tried to resolve this issue with the following:

btn->setAttribute(Qt::WA_AcceptTouchEvents);

And

qApplication.setAttribute(Qt::AA_SynthesizeMouseForUnhandledTabletEvents);

But it didn't help. I think I am missing a very small issue and after that Qt will handle all the touch related events on it's own and will show the right behavior.

I am cross-compiling on my Ubuntu machine using MXE. Qt version is 5.12.

2

There are 2 best solutions below

4
Former contributor On

I think I am missing a very small issue and after that Qt will handle all the touch related events on it's own and will show the right behavior.

QWidget subclasses, including QToolButton, do not handle touch events on its own. It is not even needed that Qt emulate mouse events. You are seeing the effects in your application of emulated mouse events synthesized by Windows. This makes possible for ancient Windows programs designed to be used with mouse will work somewhat in modern touch enabled Windows tablets. But the mouse emulation is hardly perfect.

If the behavior is unacceptable for the QToolButton class, you have the option of subclass it and handle touchevents as you see fit, overriding mouse events as well, to avoid the synthesized events to be handled as well.

0
Baffer On

I had the same problem. I solved it by changing the signal to which the slot is connected.Previously I was connected my slot to QPushButton::pressed. To fix it, change the connect to use the signal QPushButton::clicked:

connect(my_button, &QPushButton::clicked, this, &MyClass::onMyButtonClicked);