简体   繁体   English

焦点对准后如何更改QPushButton图标?

[英]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.

相关问题 只有键盘焦点时,如何才能为glymphicon图标应用CSS样式? - How can I apply a CSS Style for a glymphicon icon ONLY when it has keyboard focus? 在“垫子输入”字段中聚焦时如何更改垫子图标的颜色? - How to change mat-icon colour when focus in mat-Input field? 当输入字段为焦点时如何更改图标颜色 输入字段来自另一个 div - HTML - CSS - How to change icon color when input field is focus input field is from another div - HTML - CSS 当子元素具有焦点时,如何使div /容器更改背景颜色? - How to make div/container change background color when child element has focus? 当contenteditable具有焦点时,更改另一个元素的样式 - Change style of another element when contenteditable has focus 焦点时突出显示或更改@ Html.DropDownList的边框或背景 - Highlight or change border or background of @Html.DropDownList when it has the focus 聚焦 nativebase TextArea 组件时如何更改边框颜色? - How to change border color when focus on a nativebase TextArea Component on focus? 专注于输入时如何更改背景位置 - how to change background position when focus on input 未聚焦时如何更改选定的选项颜色? - How to change selected option color when it is not on focus? 焦点更改输入搜索fa fa图标 - Input search on focus change fa fa icon
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM