Results 1 to 12 of 12

Thread: Setting text color on QLabel

  1. #1
    Join Date
    Jan 2006
    Posts
    12
    Qt products
    Qt/Embedded
    Platforms
    Unix/X11

    Default Setting text color on QLabel

    Qt 4.0.1: What methods can I use for setting the text color on a QLabel item? I can easily change the font, font size, the text itself and the alignment of the label but not the text colour.

    Edit:
    Ok, I managed to change the colour of the label by creating a new palette for it, but I want to change the colour of the text only, not whole labels.
    Last edited by Yorma; 13th January 2006 at 11:42.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,360
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Setting text color on QLabel

    Did you try using
    Qt Code:
    1. somelabel->setText("<font color='red'>Some text</font>");
    To copy to clipboard, switch view to plain text mode 
    ?

  3. The following 2 users say thank you to wysota for this useful post:

    deepal_de (6th April 2011), vinodpaul (31st January 2013)

  4. #3
    Join Date
    Jan 2006
    Posts
    12
    Qt products
    Qt/Embedded
    Platforms
    Unix/X11

    Default Re: Setting text color on QLabel

    Quote Originally Posted by wysota
    Did you try using
    Qt Code:
    1. somelabel->setText("<font color='red'>Some text</font>");
    To copy to clipboard, switch view to plain text mode 
    ?
    That did it, thank you. How about if i want to change the colour on run time, do I just set the color again like for example:
    Qt Code:
    1. somelabel->setText("<font color='Blue'>Some text</font>");
    To copy to clipboard, switch view to plain text mode 

    Or is there an alternative way of doing it? Can the "<font color='Blue'>" be replaced with an constant value or a class member?

  5. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,360
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Setting text color on QLabel

    It is just a string, you can replace it however and whenever you want.

  6. #5
    Join Date
    Jan 2006
    Posts
    12
    Qt products
    Qt/Embedded
    Platforms
    Unix/X11

    Default Re: Setting text color on QLabel

    Quote Originally Posted by wysota
    It is just a string, you can replace it however and whenever you want.
    Thanks!

    I'm kinda unfamiliar with that HTML style markup. How can I set the text for the label to be taken from for example QString or QTime with that kind of color color setting? How do I wrap it so that it does not show you the variables name, but what is stored in them instead?

  7. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Setting text color on QLabel

    Quote Originally Posted by Yorma
    How do I wrap it so that it does not show you the variables name, but what is stored in them instead?
    Qt Code:
    1. QString colour; // you can use also QColor
    2. QString text;
    3. // ...
    4. QString template = tr("<font color='%1'>%2</font>");
    5. somelabel->setText( template.arg( colour, text ) );
    To copy to clipboard, switch view to plain text mode 

  8. #7
    Join Date
    Jan 2006
    Location
    Kerala
    Posts
    371
    Thanks
    76
    Thanked 37 Times in 32 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Setting text color on QLabel

    Qt Code:
    1. QString colour; // you can use also QColor
    2. QString text;
    3. // ...
    4. QString template = tr("<font color='%1'>%2</font>");
    5. somelabel->setText( template.arg( colour, text ) );
    Don't use template it's a keyword
    Last edited by sunil.thaha; 14th January 2006 at 05:00.
    We can't solve problems by using the same kind of thinking we used when we created them

  9. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Setting text color on QLabel

    Quote Originally Posted by sunil.thaha
    Don't use template it's a keyword
    Good point, I didn't notice that.

  10. #9
    Join Date
    Jan 2006
    Location
    Lincoln, NE USA
    Posts
    177
    Thanks
    3
    Thanked 7 Times in 6 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Setting text color on QLabel

    Quote Originally Posted by jacek
    Qt Code:
    1. QString colour; // you can use also QColor
    2. QString text;
    3. // ...
    4. QString fonttemplate = tr("<font color='%1'>%2</font>");
    5. somelabel->setText( fonttemplate.arg( colour, text ) );
    To copy to clipboard, switch view to plain text mode 
    Simply, but elegant!

    It's hints like these that make this forum so valuable.

    Jacke, if you are ever in Lincoln, NE then visit the Nebraska State Office Building and ask for me. I'll take you out for a steak dinner!!

  11. #10
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Setting text color on QLabel

    Quote Originally Posted by GreyGeek
    if you are ever in Lincoln, NE then visit the Nebraska State Office Building and ask for me. I'll take you out for a steak dinner!!
    Thank you! Unfortunately I have no overseas journeys in my plans now, but we'll see what the future holds.

  12. #11
    Join Date
    Jan 2006
    Posts
    80
    Thanks
    1
    Qt products
    Qt/Embedded
    Platforms
    Unix/X11

    Default Re: Setting text color on QLabel

    Quote Originally Posted by wysota
    Did you try using
    Qt Code:
    1. somelabel->setText("<font color='red'>Some text</font>");
    To copy to clipboard, switch view to plain text mode 
    ?
    hi i tried this code but fails to get any result...

    Mahe2310

  13. #12
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,360
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Setting text color on QLabel

    Quote Originally Posted by mahe2310
    hi i tried this code but fails to get any result...
    'Any result'? Meaning, you get a blank label?

Similar Threads

  1. Unhandled exception in qatomic
    By NewGuy in forum Qt Programming
    Replies: 14
    Last Post: 23rd July 2013, 09:49
  2. Replies: 2
    Last Post: 23rd July 2012, 08:42
  3. Replies: 0
    Last Post: 2nd September 2008, 12:01
  4. Position of text in a qlabel
    By jiveaxe in forum Qt Programming
    Replies: 1
    Last Post: 25th May 2008, 13:26
  5. QTreeWidgetItem setting color for the text
    By arjunasd in forum Qt Programming
    Replies: 1
    Last Post: 29th August 2007, 17:22

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.