I know how to set it:
color_frame.setStyleSheet("QWidget { background-color: blue}")
But how do I read the color value (may be in hex, I don’t care) of the QtFrame background ?
Please note, I’m not looking for:
color_frame.palette().highlight().color().name()
since it doesn’t seem to ge me the value of QtFrame background color.
>Solution :
highlight() won’t give you the background since the Highlight role is used for selections.
By default, widgets use the Window role (unless specified by setBackgroundRole()) to draw the background, so you need to use the related window() function to get it:
color_frame.palette().window().color().name()
Or just use color() with the related role as argument:
color_frame.palette().color(QPalette.Window).name()
To be slightly more sure about it, use the current background role:
color_frame.palette().color(color_frame.backgroundRole()).name()
Note that:
- in some cases, the style could use the color set by the palette (including those set in the stylesheet) in slightly different ways and the drawing could differ from what’s expected; remember that palette colors are only used as reference, it’s up to the style to "decide" how strict be about that;
- setting a global property for a parent widget is normally not a good idea, if a generic (o no) selector is used: some complex widgets require that as soon as a single property is set, all others must be set too. This is notably the case of QComboBox and QScrollBar; you should use more specific selector (possibly using the class or the object name);
- the only color properties that are readable from the palette after setting a stylesheet are the
color(applies toWindowText,TextandButtonTextroles),background(Window,BaseandButton),selection-color(Highlight),selection-background-color(HighlightedText) andalternate-base(AlternateBase); all other color roles are automatically computed based on the above; any other property, specifically those set for subcontrols, are not available;