Results 1 to 6 of 6

Thread: How to set font size and color for different text on QLabel

  1. #1
    Join Date
    Mar 2013
    Location
    Hyderabad,Bangalore,India
    Posts
    70
    Thanks
    8
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default How to set font size and color for different text on QLabel

    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>" );

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: How to set font size and color for different text on QLabel

    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.

  3. #3
    Join Date
    Mar 2014
    Location
    china
    Posts
    3
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android

    Default Re: How to set font size and color for different text on QLabel

    you can call the label.setFont(font &f) to set the font in which the text is drawn to font f

  4. #4
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: How to set font size and color for different text on QLabel

    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.

  5. #5
    Join Date
    Jun 2014
    Location
    Whidbey Island, WA, USA
    Posts
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to set font size and color for different text on QLabel

    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

  6. #6
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: How to set font size and color for different text on QLabel

    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().

Similar Threads

  1. QLabel Font size is not changing???
    By Gokulnathvc in forum Newbie
    Replies: 4
    Last Post: 22nd March 2011, 11:17
  2. Replies: 1
    Last Post: 2nd August 2008, 15:46
  3. adjust font size to QLabel-size
    By hunsrus in forum Qt Programming
    Replies: 0
    Last Post: 9th July 2008, 14:33
  4. changing QLabel font size(label created on the graphicsView)
    By maverick_pol in forum Qt Programming
    Replies: 11
    Last Post: 17th August 2007, 08:36
  5. QLabel, large, rich text, font size
    By TheKedge in forum Qt Programming
    Replies: 3
    Last Post: 5th February 2007, 11:56

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.