Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 604
bastibe Avatar asked Sep 23 '09 08:09

bastibe


People also ask

How to make text bold Qt?

warning->setStyleSheet("font-weight: bold; color: red"); Qt supports most CSS styles on its QWidget -derived classes. You don't need to set the text format to Qt::RichText for this to work.

How do I change font size in Qt Designer?

Select Fonts & Colors tab. Under the Font heading after where it says Family: select Source Code Pro from the dropdown menu as marked by the mouse cursor in the below screenshot. After where it says Size: select 10 from the dropdown menu. Font size 10 is the best font size in my Qt Creator.

What is label in Qt?

QLabel is used for displaying text or an image. No user interaction functionality is provided. The visual appearance of the label can be configured in various ways, and it can be used for specifying a focus mnemonic key for another widget. A QLabel can contain any of the following content types: Content.


2 Answers

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>

like image 96
rpg Avatar answered Sep 20 '22 12:09

rpg


You can use Qt StyleSheets and set the styleSheet property of QLabel

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

Qt supports most CSS styles on its QWidget-derived classes. You don't need to set the text format to Qt::RichText for this to work.

like image 44
code-tinkerer Avatar answered Sep 17 '22 12:09

code-tinkerer