Get The Background Color Of A Widget - Really
Solution 1:
The palette is returning the correct colours.
The mistake you're probably making is that you're assuming "background" always means the same thing for all widgets. Let's take an unmodified QListWidget
as an example. On a desktop with a traditional light-coloured scheme, this will probably appear as a white viewport inside a 3D sunken panel. But if you query the "background" for this widget, you will see something like this:
>>>widget = QtGui.QListWidget()>>>widget.palette().color(QtGui.QPalette.Background).name()
'#edecec'
which is obviously not white. So Background
is the wrong color role to query for this widget. Instead, it looks like Base
might be more appropriate:
>>> widget.palette().color(QtGui.QPalette.Base).name()
'#ffffff'
It's worth noting that the documentation for color roles states that Background
and Foreground
are obsolete values, with Window
and WindowText
being recommended instead. Perhaps this is because the former terms are now considered to be misleading.
UPDATE:
On platforms which use pixmap-based styling, some of the reported palette colours will not match the visual appearance of a widget. This issue specifically affects Windows and OSX, and so may explain why you are not able to get the apparent background colour of tabs. This issue is documented in a Qt FAQ, which also gives some possible solutions (although the QProxyStyle
option is supported in PyQt5 but not PyQt4).
Post a Comment for "Get The Background Color Of A Widget - Really"