本文介绍了如何设置 QWidget 背景颜色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面代码中的行 w.setBackgroundRole(QPalette.Base) 不起作用.为什么?我该如何解决?

The line w.setBackgroundRole(QPalette.Base) in the code below has no effect. Why? How do I fix that?

import sys
from PySide.QtCore import *
from PySide.QtGui import *

app = QApplication(sys.argv)
w = QWidget()
w.setBackgroundRole(QPalette.Base)
w.show()
app.exec_()

推荐答案

您需要在小部件上调用 setAutoFillBackground(True).默认情况下,QWidget 不会填充其背景.

You need to call setAutoFillBackground(True) on the widget. By default, a QWidget doesn't fill its background.

有关详细信息,请参阅 setAutoFillBackground 的文档 财产.

For more information, see the documentation for the setAutoFillBackground property.

如果你想使用任意的背景颜色,你需要修改小部件的调色板:

If you want to use an arbitrary background color, you need to modify the widget's palette instead:

p = w.palette()
p.setColor(w.backgroundRole(), Qt.red)
w.setPalette(p)

这篇关于如何设置 QWidget 背景颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 03:31
查看更多