如何更改 QTextEdit 小部件的颜色

How to change color of QTextEdit widget

我正在尝试将 QTextEdit 的颜色更改为黑色,无论是否有文本或 none,以使其具有终端外观。在我看来,QTextEdit (PyQy4) 的默认白色背景颜色无法通过其他 Qt 小部件工作的方式进行更改。我尝试了以下方法:

w.setTextBackgroundColor(QColor(0,0,0))

w.setAutoFillBackground(True)

p = w.palette()
p.setColor(w.backgroundRole(), QColor(0,0,0))
w.setPalette(p)

一个简单的解决方案是使用 qss:

w.setStyleSheet("background-color: rgb(0, 0, 0);")

如果你想使用 QPalette 你应该将它应用到视口():

p = w.viewport().palette()
p.setColor(w.viewport().backgroundRole(), QtGui.QColor(0,0,0))
wt.viewport().setPalette(p)