Solved Change QLabel background without affecting the current style
-
@Mark81
Hi
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)
OR
- 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?
-
@Mark81
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 theQWidget
, 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. -
-
@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. -
@Mark81
Hi
I check with your stylesheetyou can do the following
add
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.
ui->theone->setProperty("active",true);
ui->theone->style()->unpolish(ui->theone);
ui->theone->style()->polish(ui->theone);seems to work ok
-
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).Before
Active
After
Perhaps it depends on the platform?
-
@Mark81
Hmm, i assume you change the Dark sheet ?If i do
QLabel {
font: xxx
}
in global sheet over the Active checkit 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.
-
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.