Important: Please read the Qt Code of Conduct -

Change QLabel background without affecting the current style

  • Under Linux, Qt5.9.2: in the main() function I set a style:

    int main(int argc, char *argv[])
        QApplication a(argc, argv);
        Widget w;
        QFile file("://stylesheet-dark-orange");;
        QString stylesheet = file.readAll();
        return a.exec();

    now I need to programmatically change the background color of a label when something happens. I cannot use the setStylesheet() function as it will replace any other settings. Hence I tried with palettes:

    QPalette palette = ui->label->palette();
    palette.setColor(ui->label->backgroundRole(), Qt::darkGreen);

    But there is no effect at all. I also set the autoFillBackground property to true.

    What's the right way to programmatically change the background color of a QLabel without affecting the other style settings?

  • Lifetime Qt Champion


    Can you show your stylesheet ?

  • The stylesheet of the whole form is this:

    But I discovered an odd behavior... it seems to me more a bug than a feature.
    I printed out the stylesheet of a QLabel:

    qDebug() << ui->label->stylesheet();

    and it returned an empty string "". But if I set the stylesheet to the same value:


    it loses some styling properties (i.e. font settings) - I guess inherit from the stylesheet above. It sounds weird to me.

  • Lifetime Qt Champion

    Style sheets are cascading so it affect all children of a parent.
    Often you set 1 stylesheet on QApplication to have it work for all
    widgets, windows and dialog application wide.
    so in short, if you set on say MainWindow, it will have effect on all widgets
    MainWindow owns.

  • Yes, I know this, but:

    • it should be fully retrieved in each children (instead of an empty string)


    • setting a stylesheet when one is already inherited from the parent should lead to override only the properties set (hence: empty string overrides nothing, QLabel background overrides QLabel background only, etc...)

    Back to the original question: when setting a general stylesheet for the whole application, how am I supposed to override a specific property programmatically?

  • Lifetime Qt Champion

    No, cascading do NOT mean it will copy the style string to each children.
    It doesn't work that way. that would be extremely wasteful.

    The issue in your case here is that when a QLabel is under stylesheet
    effect, it no longer uses its own palette, but the stylesheets. and hence
    the palette.setColor do not work.

    You might be able to change it using #name selector when event happens.
    I assume it ONE that you want to change, not all QLabels?

  • Exactly. I have plenty of QLabel on the QWidget, but I'm interested to override the background (or any other properties) of only one of those.

  • Hi, @Mark81. You can set the stylesheet for a particular widget with the setStyleSheet() function. You just add the bits you want to see in this widget.
    Other than that you use the selectors which you can find in the style sheet documentation here.
    You should find all you need about style sheets in that document.

  • @mrjj , @kenchan ,

    I'm going to try.
    To be sure: are you suggesting to change the high-level stylesheet adding a #name selector instead of changing the stylesheet of the specific object only? Did I understand correctly?

  • @Mark81
    Either should work. Just use the one which works best for you :-). I use both.
    You can do the equivalent of setStyleSheet() on a widget using Designer in the properties for that widget.

  • Lifetime Qt Champion

    I check with your stylesheet

    you can do the following
    QLabel[active=true] {
    background-color: rgb(255, 170, 0);
    very last in (global) stylesheet.

    then when you want to change it to show other background.


    seems to work ok
    alt text

  • It's a very good solution!!! But... the behavior is exactly the same :( I lose the font settings.
    Here some screenshots, sorry for the quality but I'm not able to dump the framebuffer (yet).


    Perhaps it depends on the platform?

  • Lifetime Qt Champion

    Hmm, i assume you change the Dark sheet ?

    If i do
    QLabel {
    font: xxx
    in global sheet over the Active check

    it still stays the same with active flipflop

    So how did you (if you did ) change its font in first place?

  • mmm... you're right. Now I (partially) understand. I set the font in the main QWidget and because it worker I thought it had priority over the stylesheet (and it should, because is a setting that differs than the default value).

    Moving the font customization inside the stylesheet did the trick.

    Perhaps is by design, but it's a bit counter-intuitive: if I explicitly set a property in the Designer for an object it should override any stylesheet setting. It works only on startup but reloading the stylesheet leads to a different behavior - from there my confusion.

  • Lifetime Qt Champion

    The style sheet introduction documentation states that the style sheet customisation will be applied on top of the current widget style. That is the important part. For example, you can modify the palette of your application, the style sheet takes precedence.

Log in to reply