PDA

View Full Version : Setting text color on QLabel



Yorma
13th January 2006, 12:33
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.

wysota
13th January 2006, 12:50
Did you try using
somelabel->setText("<font color='red'>Some text</font>"); ?

Yorma
13th January 2006, 13:11
Did you try using
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:
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?

wysota
13th January 2006, 13:28
It is just a string, you can replace it however and whenever you want.

Yorma
13th January 2006, 13:52
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?

jacek
13th January 2006, 15:56
How do I wrap it so that it does not show you the variables name, but what is stored in them instead?


QString colour; // you can use also QColor
QString text;
// ...
QString template = tr("<font color='%1'>%2</font>");
somelabel->setText( template.arg( colour, text ) );

sunil.thaha
14th January 2006, 05:50
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

jacek
14th January 2006, 20:04
Don't use template it's a keyword
Good point, I didn't notice that.

GreyGeek
24th January 2006, 18:14
QString colour; // you can use also QColor
QString text;
// ...
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!!

jacek
24th January 2006, 22:02
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.

mahe2310
15th June 2006, 08:15
Did you try using
somelabel->setText("<font color='red'>Some text</font>"); ?

hi i tried this code but fails to get any result...

Mahe2310

wysota
15th June 2006, 08:25
hi i tried this code but fails to get any result...

'Any result'? Meaning, you get a blank label?