问题
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?
回答1:
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; }");
回答2:
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)
来源:https://stackoverflow.com/questions/1531062/how-to-change-a-qpushbutton-icon-when-it-has-focus