Qt - QPushButton text formatting(Qt - QPushButton 文本格式)
问题描述
我有一个 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
这篇关于Qt - QPushButton 文本格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Qt - QPushButton 文本格式
基础教程推荐
- 如何通过C程序打开命令提示符Cmd 2022-12-09
- 如何将 std::pair 的排序 std::list 转换为 std::map 2022-01-01
- 我有静态或动态 boost 库吗? 2021-01-01
- C++结构和函数声明。为什么它不能编译? 2022-11-07
- 这个宏可以转换成函数吗? 2022-01-01
- 常量变量在标题中不起作用 2021-01-01
- 如何在 C++ 中初始化静态常量成员? 2022-01-01
- 静态库、静态链接动态库和动态链接动态库的 .lib 文件里面是什么? 2021-01-01
- 如何检查GTK+3.0中的小部件类型? 2022-11-30
- 在 C++ 中计算滚动/移动平均值 2021-01-01
