Qt - QPushButton 文本格式

Qt - QPushButton text formatting(Qt - QPushButton 文本格式)
本文介绍了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 文本格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Rising edge interrupt triggering multiple times on STM32 Nucleo(在STM32 Nucleo上多次触发上升沿中断)
How to use va_list correctly in a sequence of wrapper functions calls?(如何在一系列包装函数调用中正确使用 va_list?)
OpenGL Perspective Projection Clipping Polygon with Vertex Outside Frustum = Wrong texture mapping?(OpenGL透视投影裁剪多边形,顶点在视锥外=错误的纹理映射?)
How does one properly deserialize a byte array back into an object in C++?(如何正确地将字节数组反序列化回 C++ 中的对象?)
What free tiniest flash file system could you advice for embedded system?(您可以为嵌入式系统推荐什么免费的最小闪存文件系统?)
Volatile member variables vs. volatile object?(易失性成员变量与易失性对象?)