Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
500 views
in Technique[技术] by (71.8m points)

c++ - How to create a bold, red text label in Qt?

I want to write a single, bold red line in my application using 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 *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". What am I doing wrong?

Thank you for your help.

question from:https://stackoverflow.com/questions/1464591/how-to-create-a-bold-red-text-label-in-qt

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Try using HTML formatting: <b><font... etc </b>.

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...