How to change a QPushButton icon when it has focus?

瘦欲@ 提交于 2019-12-10 12:24:18

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!