以下代码中的行w.setBackgroundRole(QPalette.Base)
没有效果。为什么?我该如何修复它?
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
不会填充其背景。setAutoFillBackground
属性的文档。p = w.palette()
p.setColor(w.backgroundRole(), Qt.red)
w.setPalette(p)
您也可以使用 setStyleSheet
,例如:
w.setAttribute(Qt.Qt.WA_StyledBackground, True)
w.setStyleSheet('background-color: red;')
QPalette::Window
角色就被用来填充它的背景。否则,会使用你的小部件的backgroundRole
代替QPalette::Window
,所以你的代码应该按预期工作。 - jmk