You are not logged in.

Dear visitor, welcome to QtForum.org. If this is your first visit here, please read the Help. It explains in detail how this page works. To use all features of this page, you should consider registering. Please use the registration form, to register here or read more information about the registration process. If you are already registered, please login here.

hoffhoff

Beginner

  • "hoffhoff" is male
  • "hoffhoff" started this thread

Posts: 3

Location: Brazil

Occupation: Computer Engineer

  • Send private message

1

Monday, October 10th 2011, 9:26pm

QLabel with border color different of the text color

Hello,
I have a class called TCalendarButton and it inherits from QLabel. The idea of this class is to represent the days of a calendar. So, on holidays, the text is supposed to be red, but when the number is pressed a green border around the number is supposed to be shown.

The border is created using this:

Source code

1
setFrameStyle( Box | Plain );


It is working for the normal days, but for the holidays, when I do:

Source code

1
2
3
QPalette palet;
palet.setColor( QPalette::WindowText, Qt::red );
setPalette( palet );


The border of this number becomes red too. I've tried some different roles in palette's setColor but I couln't find a way to make the boarder color to be different of the text color.

I could change the text color using the:

Source code

1
<font color=...></font>


In the text, but I wouldn't like to change the text of the label, as the code is too big and I'm affraid of side effects. I've thought about rewriting the "text()" function to remove the <font> tag, but the QLabel's "text()" is not virtual.

Is there an easy way to make the text color and the font color differents for this situation?

Thanks,
Renan Greinert
Simple tips about C++:
http://renangreinert.wordpress.com

slevon

Trainee

Posts: 90

Location: Germany

Occupation: electrical engineer

  • Send private message

2

Tuesday, October 11th 2011, 8:04am

Hi,
have a look at the Qt-Stylesheets. It's much more powerful and way easier than Palettes.

http://doc.qt.nokia.com/latest/stylesheet.html

Hope this helps,
slevon

hoffhoff

Beginner

  • "hoffhoff" is male
  • "hoffhoff" started this thread

Posts: 3

Location: Brazil

Occupation: Computer Engineer

  • Send private message

3

Tuesday, October 11th 2011, 2:34pm

Thank you slevon!

I didn't find a way to change the text color via StyleSheet. I've tried:

setStyleSheet("text: red;");

but it didn't work.

Anyway, I could solve my problem by doing:

Source code

1
2
3
4
5
6
  QPalette palet;
  palet.setColor( QPalette::WindowText, textColor );
  setPalette( palet );

  QString tmplt = tr("border: 1px solid %1;");
  setStyleSheet( tmplt.arg( borderColor.name()) );


Thanks again,
Renan
Simple tips about C++:
http://renangreinert.wordpress.com

slevon

Trainee

Posts: 90

Location: Germany

Occupation: electrical engineer

  • Send private message

4

Tuesday, October 11th 2011, 3:03pm

Hi,
nice if you have a working solution, but wouldn't it be easier to simply call:

Source code

1
2
//sets the Text to green and the border to red
setStyleSheet("color: rgb(0,255,0) ; border: 1px solid  #ff0000 ;");


Where "color" sets the text, and "border" sets the border.
Like you would do in a css-file you can use the "#rrggbb" or the rgb(r,g,b) syntax, where the first one is more common.

Or if you want to use a QColor

Source code

1
2
//sets the Text to green and the broader to the given color 
setStyleSheet("color:#00ff00; border: 1px solid "+color.name()+";");


Have a look here to figure out which properties are supported:
http://doc.qt.nokia.com/latest/styleshee…t-of-properties

Best wishes,
slevon

This post has been edited 2 times, last edit by "slevon" (Oct 11th 2011, 3:12pm)


hoffhoff

Beginner

  • "hoffhoff" is male
  • "hoffhoff" started this thread

Posts: 3

Location: Brazil

Occupation: Computer Engineer

  • Send private message

5

Tuesday, October 11th 2011, 5:22pm

Much more beautiful and worked like a charm.
Thank you again.

All the best,
Renan Greinert
Simple tips about C++:
http://renangreinert.wordpress.com