How to change button style on touch event
-
wrote on 5 Jul 2024, 07:20 last edited by
Hello,
We have an application on Qt6 with an user interface. User can interact either with the mouse either by touch screen.
When user clicks on a QPushButton we change its style (to show that the button has been clicked) using style sheet in the .ui. For example :
QPushButton:pressed {
icon:url(":/UI/Icons/photo-down.png");
}This style sheet only applies on a mouse click. How to apply the same style sheet when user press touch screen ?
-
wrote on 16 Jul 2024, 08:51 last edited by
Hello,
Anyone can help us ?
Do you need more information to understand our need ? -
wrote on 16 Jul 2024, 09:25 last edited by JonB
@ariari
While you await a better answer, if anyone knows such.FWIW, https://stackoverflow.com/questions/28148805/qt-qpushbutton-style-sheet-properties-not-applied from 9 years ago asked the same question and received no solution. Since I do not see anywhere in the stylesheet docs support for any kind of "touched", only
:pressed
, I presume Qt stylesheet simply does not have support for the former. (I assume your:pressed
does not work for a touch.) I offer the following as considerations:-
If you subclass these push buttons (or use
eventFilter()
you couldemit btn->pressed()
(and correspondingly on release). I do not know whether there are nasty consequences for this, or even if it would work. -
My other thought is, if there are no better solutions, you might use dynamic properties on the button with a corresponding CSS rule. That is documented in https://doc.qt.io/qt-6/stylesheet-syntax.html#selector-types, https://doc.qt.io/qt-6/stylesheet-examples.html#customizing-using-dynamic-properties, https://wiki.qt.io/Dynamic_Properties_and_Stylesheets. You would invent your own property, like
touchPressed
, which you would set and clear on touch actions. You should then be able to use that in your CSS styling rules. You may need to do some judiciousunpolish()
/polish()
calls. I have used this in the past, though not for your case.
-