简体   繁体   English

如何在 Qt 中创建一个粗体的红色文本标签?

[英]How to create a bold, red text label in Qt?

I want to write a single, bold red line in my application using Qt.我想使用 Qt 在我的应用程序中编写一条粗体红线。

As far as I understand, I would create a QLabel, set its textFormat to rich text and give it a rich text string to display:据我了解,我会创建一个 QLabel,将其 textFormat 设置为富文本并为其提供一个富文本字符串以显示:

QLabel *warning = new QLabel;
warning->setTextFormat(Qt::RichText);
warning->setText("{\\rtf1\\ansi\\ansicpg1252 {\\fonttbl\\f0\\fswiss\\fcharset0 Helvetica;} {\\colortbl;\\red255\\green0\\blue0;} \\f0 \\cf0 this is bold red text}");

I tested this rich text string in a rich text editor and it displays fine.我在富文本编辑器中测试了这个富文本字符串,它显示正常。

But Qt displays the whole string with all braces, keywords and backslashes instead of "this is bold red text".但是 Qt 会显示带有所有大括号、关键字和反斜杠的整个字符串,而不是“这是粗体红色文本”。 What am I doing wrong?我究竟做错了什么?

Thank you for your help.谢谢您的帮助。

Try using HTML formatting: <b><font... etc </b> .尝试使用 HTML 格式: <b><font... etc </b>

Qt Designer does it like this: <span style=" font-size:8pt; font-weight:600; color:#aa0000;">TextLabel</span> Qt Designer 是这样做的: <span style=" font-size:8pt; font-weight:600; color:#aa0000;">TextLabel</span>

You can use Qt StyleSheets and set the styleSheet property of QLabel您可以使用Qt StyleSheets并设置QLabelstyleSheet属性

warning->setStyleSheet("font-weight: bold; color: red");

Qt supports most CSS styles on its QWidget -derived classes. Qt 在其QWidget派生类上支持大多数 CSS 样式。 You don't need to set the text format to Qt::RichText for this to work.您无需将文本格式设置为Qt::RichText即可使其工作。

Qt 使用一个简单的HTML 子集进行格式化。

You can also do it programmatically using the settext function.您也可以使用settext函数以编程方式执行此settext Something like this:像这样的东西:

QString labelText = "<P><b><i><font color='#ff0000' font_size=4>";
labelText .append("Text what u want to display");
labelText .append("</font></i></b></P></br>");
QLabel label->setText(labelText);

You can do it in a single line as well.您也可以在一行中完成。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM