[英]How to change a QPushButton icon when it has focus?
I'm using a QPushButton in an S60 Qt app. 我在S60 Qt应用程序中使用QPushButton。
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. 我还尝试了其他方法,例如使用QPalette等,但是无法正常工作。
Is there a way to select a focus style for a widget? 有没有一种方法可以为小部件选择焦点样式?
...or is there another way to do this? ...或者还有另一种方法吗?
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; }");
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) 1)子类化QPushButton,并重新实现focusInEvent和focusOutEvent(受保护的成员)
2) Another approach, is to do "void QPushButton::installEventFilter ( QObject * filterObj )" on Push Button instance (filterObj will implement custom focus events handling) 2)另一种方法是在Push Button实例上执行“ void QPushButton :: installEventFilter(QObject * filterObj)”(filterObj将实现自定义焦点事件处理)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.