Qt - QPushButton 文本格式

2023-01-22C/C++开发问题
2

本文介绍了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 文本格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

无法访问 C++ std::set 中对象的非常量成员函数
Unable to access non-const member functions of objects in C++ std::set(无法访问 C++ std::set 中对象的非常量成员函数)...
2024-08-14 C/C++开发问题
17

从 lambda 构造 std::function 参数
Constructing std::function argument from lambda(从 lambda 构造 std::function 参数)...
2024-08-14 C/C++开发问题
25

STL BigInt 类实现
STL BigInt class implementation(STL BigInt 类实现)...
2024-08-14 C/C++开发问题
3

使用 std::atomic 和 std::condition_variable 同步不可靠
Sync is unreliable using std::atomic and std::condition_variable(使用 std::atomic 和 std::condition_variable 同步不可靠)...
2024-08-14 C/C++开发问题
17

在 STL 中将列表元素移动到末尾
Move list element to the end in STL(在 STL 中将列表元素移动到末尾)...
2024-08-14 C/C++开发问题
9

为什么禁止对存储在 STL 容器中的类重载 operator&()?
Why is overloading operatoramp;() prohibited for classes stored in STL containers?(为什么禁止对存储在 STL 容器中的类重载 operatoramp;()?)...
2024-08-14 C/C++开发问题
6