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.
Re: Setting text color on QLabel
Did you try using
Code:
somelabel->setText("<font color='red'>Some text</font>");
?
Re: Setting text color on QLabel
Quote:
Originally Posted by wysota
Did you try using
Code:
somelabel->setText("<font color='red'>Some text</font>");
?
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:
Code:
somelabel->setText("<font color='Blue'>Some text</font>");
Or is there an alternative way of doing it? Can the "<font color='Blue'>" be replaced with an constant value or a class member?
Re: Setting text color on QLabel
It is just a string, you can replace it however and whenever you want.
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?
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?
Code:
QString colour;
// you can use also QColor // ...
QString template = tr
("<font color='%1'>%2</font>");
somelabel->setText( template.arg( colour, text ) );
Re: Setting text color on QLabel
Quote:
Qt Code:
- QString colour; // you can use also QColor
- QString text;
- // ...
- QString template = tr("<font color='%1'>%2</font>");
- somelabel->setText( template.arg( colour, text ) );
Don't use template it's a keyword
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.
Re: Setting text color on QLabel
Quote:
Originally Posted by jacek
Code:
QString colour;
// you can use also QColor // ...
QString fonttemplate
= tr
("<font color='%1'>%2</font>");
somelabel->setText( fonttemplate.arg( colour, text ) );
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!!
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.
Re: Setting text color on QLabel
Quote:
Originally Posted by wysota
Did you try using
Code:
somelabel->setText("<font color='red'>Some text</font>");
?
hi i tried this code but fails to get any result...
Mahe2310
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?