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

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

相关文档推荐

Bring window to front -gt; raise(),show(),activateWindow() don’t work(把窗户放在前面 -raise(),show(),activateWindow() 不起作用)
How to get a list video capture devices NAMES (web cameras) using Qt (crossplatform)? (C++)(如何使用 Qt(跨平台)获取列表视频捕获设备名称(网络摄像机)?(C++))
How to compile Qt as static(如何将 Qt 编译为静态)
C++ over Qt : Controlling transparency of Labels and Buttons(C++ over Qt:控制标签和按钮的透明度)
How to know when a new USB storage device is connected in Qt?(Qt如何知道新的USB存储设备何时连接?)
What is an event loop in Qt?(Qt 中的事件循环是什么?)