获取小部件的背景颜色 - 真的 [英] Get the background color of a widget - really

查看:38
本文介绍了获取小部件的背景颜色 - 真的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法获取小部件的实际背景颜色.在我的特殊情况下,我在使用 QTabWidget 中的小部件时遇到了问题.

I'm having trouble getting the actual background color of widgets. In my special case I'm having trouble with widgets within a QTabWidget.

这是在Windows7上.因此,经典的小部件具有一些灰色背景,而选项卡中的小部件通常以白色背景绘制.

This is on Windows7. So classic widgets have some greyish background, whereas widgets within a tab are generally drawn with a white background.

我尝试过:

def bgcolor(widget):
    color = widget.palette().color(widget.backgroundRole()) # version 1
    color = widget.palette().color(QtGui.QPalette.Background) # version 2
    rgba = color.red(), color.green(), color.blue(), color.alpha()
    return rgba

这几乎是我可以从Qt文档中弄清楚的自己以及google和SO提供的内容.但是,这根本行不通.

which is pretty much what I could figure out myself from the Qt documentation and what google and SO give. But, this just doesn't work.

我正在测试TabWidget内部和外部的窗口小部件,并且上面的函数针对明显不同颜色的窗口小部件返回相同的颜色.即,即使对于选项卡中的纯白色小部件,它也始终返回灰色.

I'm testing widgets inside and outside of a TabWidget and the function above returns identical colors for obviously differently colored widgets. Namely it always returns the greyish color, even for the plain white colored widgets within the tab.

我在这里遗漏了什么吗?

Am I missing something here?

我的问题是,当使用matplotlib时,当父部件为白色时,matplotlib嵌入到QTabWidget中时,会使用错误的背景颜色绘制带有无"面色的图形:灰色.

My problem is when using matplotlib, matplotlib draws figures with a "none" facecolor with the wrong background color when embedded in a QTabWidget: grey, even though the parent widget is white.

要解决此问题,我想获取小部件的背景色并将其设置为图形的背景色.尽管这可能是matplotlib问题,但我猜想这将是最快的解决方法.当我注意到我无法获得正确的颜色时,我变得坚持:)

To fix this I wanted to get the background color of the widget and set it as background color for the figure. Though this may be a matplotlib issue, I guessed this would be the quickest workaround. As I noticed I couldn't get the proper color, I became insistent :)

推荐答案

调色板返回正确的颜色.

The palette is returning the correct colours.

您可能犯的错误是您假设背景"对于所有小部件来说总是意味着相同的事情.让我们以未修改的 QListWidget 为例.在具有传统浅色方案的桌面上,这可能会在3D凹陷面板内显示为白色视口.但是如果你查询这个小部件的背景",你会看到这样的:

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'

显然不是白色.所以 Background 是查询这个小部件的错误颜色角色.相反,它看起来 Base 可能更合适:

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'

值得注意的是,有关颜色角色的文档 指出 BackgroundForeground 是过时的值,推荐使用 WindowWindowText 代替.也许这是因为以前的术语现在被认为具有误导性.

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.

更新:

在使用基于像素图的样式的平台上,某些报告的调色板颜色将与小部件的视觉外观不匹配.此问题特别影响 Windows 和 OSX,因此可以解释为什么您无法获得选项卡的明显背景颜色.此问题记录在 Qt常见问题解答中,该解决方案也给出了一些答案(虽然如此)PyQt5支持 QProxyStyle 选项,但PyQt4不支持.

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).

这篇关于获取小部件的背景颜色 - 真的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆