PDA

View Full Version : How to set font size and color for different text on QLabel



PstdEr
14th April 2014, 20:38
Hi,

I need to set the font size and color for the text in QLabel,the problem is that the first text should be of some size and one color ,
the other text is of some different size and different color.

the text is decided dynamically

can somebody please post the sample code ...

some thing like this , but without span style ,
setText( "<span style='font-size:18pt; font-weight:600; color:#ffffff;'>text1</span><span style='font-size:10pt; font-weight:600; color:#00aa00;'>text2</span>" );

d_stranz
15th April 2014, 17:42
Use Rich Text format to specify the font and colors for different parts of the string. Rich text is a subset of HTML. See the documentation for QLabel::text() and this link (http://qt-project.org/doc/qt-4.8/richtext-html-subset.html).

lzyinformation
16th April 2014, 02:29
you can call the label.setFont(font &f) to set the font in which the text is drawn to font f

d_stranz
16th April 2014, 15:53
you can call the label.setFont(font &f) to set the font in which the text is drawn to font f

You didn't understand the original question. The OP wants to change the font and text color for different parts of the same label. Your solution only lets him change the font for the entire label, not only a part of it.

dieven
16th August 2014, 16:19
I'd like to use the colors from my QLabel's current StyleSheet for formatting

The QLabel StyleSheet has something like color:blue;selection-color:red

I'd like to call QLabel's setText with a string like <font color=blue>foo</font><font color=red>bar</font>, but have the colors "blue" and "red" come from the QLable's style sheet. I see that most of the CSS style sheet selectors are supported, but the syntax for this escapes me. I'm need something like <font color=styleSheetSelectorForWindowTextColor>foo</font><font color=styleSheetSelectorForHighlightTextColor>bar</font>

Thanks!

Further to my prior post, if the QLabel's style sheet is changed after setting text, I'd like the Qlabel appearance (colors) to change without having to setText again

d_stranz
18th August 2014, 21:48
You have to format your HTML string before calling setText(), right? So retrieve the colors you want from the style sheet and format them into the string along with the label's text.


I'd like the Qlabel appearance (colors) to change without having to setText again

Don't think this is possible. Calling setStyleSheet() will cause an automatic repaint, but since you're using the style sheet in a non-standard way, the painting won't change what you display in your formatted string.

Do you have some type of editor for the style sheet? If not, how is the style sheet changed? My solution would be to implement a signal that notifies of changes to the style sheet (emitted by the editor or whatever other code changes the style sheet) and a signal in the parent widget that contains the label you want to update. This slot retrieves the changed settings, reformats the label, then calls setText().