Qt - QPushButton 文本格式

2021-12-09 00:00:00 qt button formatting c++

我有一个 QPushButton,上面有一个文本和图标.我想让按钮上的文字变成粗体和红色.查看其他论坛,用谷歌搜索并失去了我的希望.如果按钮有图标,似乎没有办法做到这一点(当然,如果您不创建一个文本+前图标的新图标).这是唯一的方法吗?有人有更好的主意吗?

I have a QPushButton and on that I have a text and and icon. I want to make the text on the button to be bold and red. Looked at other forums, googled and lost my hope. Seems there is no way to do that if the button has an icon (of course if you don't create a new icon which is text+former icon). Is that the only way? Anyone has a better idea?

推荐答案

你真的不需要子类来改变按钮的格式,而是使用样式表,例如

You really don't need to subclass to change the formatting of your button, rather use stylesheets e.g.

QPushButton {
    font-size: 18pt;
    font-weight: bold;
    color: #ff0000;
}

将此应用于要更改的按钮将使按钮文本变为 18pt、粗体和红色.您可以通过 widget->setStyleSheet()

Applying this to the button that you want to change will make the buttons text 18pt, bold and red. You can apply via widget->setStyleSheet()

将此应用于上面层次结构中的小部件将为下面的所有按钮设置样式,QT 样式表 机制非常灵活且文档齐全.

Applying this to a widget in the hierarchy above will style all the buttons underneath, the QT stylesheet mechanism is very flexible and fairly well documented.

您也可以在设计器中设置样式表,这将立即为您正在编辑的小部件设置样式

You can set stylesheets in the designer too, this will style the widget that you are editing immediately

相关文章