C++ 如何在 Qt 应用程序中使用样式表更改 QPushButton 图标

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2675925/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 00:18:55  来源:igfitidea点击:

How to change QPushButton icon using stylesheets in Qt app

c++qtstylesheet

提问by Robin

Is it possible to set and change the icon on a QPushButton using stylesheets?

是否可以使用样式表设置和更改 QPushButton 上的图标?

I need this for a Windows-based white-label Qt4.5 application that customers stylize using stylesheets.

对于客户使用样式表进行样式化的基于 Windows 的白标签 Qt4.5 应用程序,我需要它。

Thanks.

谢谢。

回答by Kim

A little late for a reply, but in case anyone else pops on this via google;

回复有点晚了,但以防万一其他人通过谷歌弹出;

You can add the icon by changing the QAbstractButton-property. Simply use:

您可以通过更改 QAbstractButton 属性来添加图标。只需使用:

QAbstractButton {
     qproperty-icon: theme_url("/button_action/add");
}

The type-selector (QAbstractButton), enables you to set the style for the selected object andall its subclasses. Since QProcessButton extends the QAbstractButton, this will do the trick. (Of course you could also use the QPushButtontype-selector here).

类型选择器 (QAbstractButton) 使您能够为所选对象及其所有子类设置样式。由于 QProcessButton 扩展了 QAbstractButton,这将起到作用。(当然你也可以在QPushButton这里使用类型选择器)。

For more information on how to use selectors, I found thisquite helpful.

有关如何使用选择器的更多信息,我发现很有帮助。

If you want to learn more about the button-properties, refer to the QT-Documentation here.

如果您想了解有关按钮属性的更多信息,请参阅此处的 QT 文档。

PS. In case you want to change the icon, once the button is clicked, use

附注。如果要更改图标,单击按钮后,请使用

qproperty-icon: url(":/Icons/before_click.png") off,
                url(":/Icons/after_click.png") on ;

回答by Brian Roach

Yup.

是的。

border-image: url(/url/to/image);