Stylesheet doesn't use widget's palette for color?
-
Suppose I have a widget with some palette set for it. I've used qDebug to be sure that it has the palette I want set. When I call
widget->setStyleSheet("background-color : palette(alternate-base)")
it uses the application's palette for the alternate-base, not the widget's. The documentation says that it should reference the widget's palette.(Note, I tried searching for this, but the search function on the forum is a bit wonky and returned tons of unrelated results, so sorry if this has already been well answered)
-
I've never used the palette and style sheets together so I don't have an exact answer for you, but from the docs on qwidget palettes:
Under QWidget::palette():
Warning: Do not use this function in conjunction with Qt Style Sheets. When using style sheets, the palette of a widget can be customized using the "color", "background-color", "selection-color", "selection-background-color" and "alternate-background-color".So I would stick to either modifying the palette of a widget OR modifying it's style sheet, but not both.
I will also say the docs are contradictory though since in the Qt Style Sheet docs it does indeed show palette(xxx) as being usable in a style sheet and does indeed say that it is the "QWidget's" palette. But then says not to use palette and style sheets together under the QWidget docs.
My guess is that it isn't expecting that palette to have been modified and so it isn't picking it up. Just a guess though. Lots of ways around this problem, you can simply load the color and pass it as an RGB or #XXXXXX or even by name, instead of using the palette(...) in the style sheet.
-
@ambershark
Well the reason I'd like to use them together is the following:
Suppose I have 4 widgets of the same class. I'd like each to, say, setbackground-color : palette(light)
. But I want each widget to have a different base-color (like a red, green, blue, and yellow each).It seems like the obvious solution is for them all to have the same stylesheet, in that they use the light palette tone for background-color... but each uses a different palette to define what palette color it is.
I had missed the comment on QWidget::palette(), and I'd seen the stylesheet doc saying that they are usable together, so I guess that's my confusion around the documentation. :\
-
@shavera Oh yea like I said the docs are contradictory since in one spot it implies you can use the palette with a style sheet and in the next it uses a big warning not to do just that.
I would bug report it and see what the behavior should be. At least they should fix the documentation if not actually fix what might be a bug.
To fix your problem, instead of a palette, just use a
QMap<SomeEnumDefiningColor, QColor>
. Then you can just access whatever color you need based on widget without having to rely on potentially buggy Qt code.Or you could use a shared style sheet with just a custom addition for background-color per custom widget. So you could do:
myWidget->setStyleSheet(myWidget->styleSheet() + "QWidget { background-color: white }");
In the above SS QWidget {} may not be the right thing for you, it was just an example.
It sounds like it is potentially buggy though. I would definitely report it. Also Qt5.5 just came out, you may want to try that and see if it fixes the behavior.
-
I understand you want to use the palettes but just as a note - you can also style a widget by name and that
way override the color.QPushButton#TheWidgetName {
background-color: qlineargradient(spread:pad, x1:0, y1:0, x2:1, y2:1, stop:0 rgba(255, 255, 255, 0));
}