Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to get backgroud color of QtFrame in PyQt5?

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 ?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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 to WindowText, Text and ButtonText roles), background(Window, Base and Button), selection-color (Highlight), selection-background-color (HighlightedText) and alternate-base (AlternateBase); all other color roles are automatically computed based on the above; any other property, specifically those set for subcontrols, are not available;
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading