tags:

views:

1057

answers:

2

I'm using a QPushButton in an S60 Qt app.

I want the button icon to change when it has focus.

I've tried this...

navButton->setStyleSheet("background-image: url(c:/Data/navArrowsNotSelected.png);");
setStyleSheet("DoubleViewWidget QPushButton:focus {background-image: url(c:/Data/navArrowsSelected.png); border: 2px solid yellow}");

...but the background image doesn't change with focus.

I've also tried other approaches, using QPalette, etc., but can't get this to work.

Is there a way to select a focus style for a widget?

...or is there another way to do this?

+3  A: 

You can't set one stylesheet directly on the button, and one on the parent widget. The button one will be prefered. Try this:

 navButton->setStyleSheet("QPushButton { background-color:red; } QPushButton:focus { background-color:blue; }");
Lukáš Lalinský
Thanks! That worked: navButton->setStyleSheet("QPushButton { background-image: url(c:/Data/myIconNotSelected.png); } QPushButton:focus { background-image: url(c:/Data/myIconSelected.png); border: 2px solid yellow; }");
Sam Dutton
A: 

I can't say anything about doing this through stylesheet, but you can use next approaches:

1) Subclassing QPushButton, and reimplement focusInEvent and focusOutEvent (protected members)

2) Another approach, is to do "void QPushButton::installEventFilter ( QObject * filterObj )" on Push Button instance (filterObj will implement custom focus events handling)

vnm