2

I want to set some style properties via setStylesheet, e.g. a border

label.setStylesheet("border: 1px solid white;");

After that my label has a white border but all font properties set in the parent widget (QDesigner) are ignored!

qDebug() << label->font().family();
qDebug() << label->font().rawName();

both print the right font family but this is not applied after the setStylesheet function was called.

Same thing with colors. Colors set via QPlatte in the Designer are not used if I set some other properties via setStylesheet().

I don't know but it seems that we should not mix both technologies or I am doing something wrong here.

Pascal
  • 2,059
  • 3
  • 31
  • 52

1 Answers1

2

Unfortunately, setting one property in the style sheet of a widget usually results in all styles properties needing to be set, as well as breaking inheritance for any of those properties. I couldn't reproduce the font inheritance issue in my own environment (what version of Qt are you using?), but the following code should get you around this issue.

//  Get the color of the label's parent (this).
QColor color = this->palette().windowText().color();
QString colorString = "rgb(" +
                      QString::number( color.red() ) + "," +
                      QString::number( color.green() ) + "," +
                      QString::number( color.blue() ) + ")";

//  Get the Font of the label's parent (this).
QFont font = this->font();

//  Set the Font and Color.
label->setFont( font );
label->setStyleSheet( "color: " + colorString + "; border: 1px solid white;" );

Personally, I try to keep all of my styling in either the form editor for specific form object styles, or in a style sheet that is loaded in at the top level, much like CSS for a web page.

MildWolfie
  • 2,492
  • 1
  • 16
  • 27