Qwidget Does Not Draw Background Color

QWidget does not draw background color

Set the WA_StyledBackground attribute on the container widget:

ui = MainWindow()
ui.setupUi(mainwindow)
ui.widget.setAttribute(QtCore.Qt.WA_StyledBackground, True)

(PS: for performance reasons, this attribute isn't set by default for some widget classes - but the documentation doesn't seem to specifiy which ones).

QWidget background color does not fully cover the QWidget

Found out the solution. It seems that you need to set the background color at the QMdiSubWindow, not at QWidget. Don't know why, but it seems logical.

How to set QWidget background color?

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

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)

Set background colour for a custom QWidget

The simplest fix is this:

class AlarmWidget(QWidget):
def __init__(self, alarm, parent=None):
...
self.setAttribute(QtCore.Qt.WA_StyledBackground, True)
self.setStyleSheet('background-color: red')

This issue happens whenever a stylesheet is applied to a custom widget or to one of its ancestor widgets. To quote from the QWidget.setPalette documentation:

Warning: Do not use this function in conjunction with Qt Style Sheets.
When using style sheets, the palette of a widget can be customized
using the "color", "background-color", "selection-color",
"selection-background-color" and "alternate-background-color".

What this fails to mention, however, is that for performance reasons custom widgets have stylesheet support disabled by default. So to get your example to work properly, you must (1) set the background colour via a stylesheet, and (2) explicitly enable stylesheet support using the WA_StyledBackground widget attribute.

A minimal example that demonstrates this is shown below:

import sys
from PyQt5 import QtCore, QtWidgets

class AlarmWidget(QtWidgets.QWidget):
def __init__(self, alarm, parent=None):
super(AlarmWidget, self).__init__(parent)
self.setFixedHeight(200)
# self.setAutoFillBackground(True)
# p = self.palette()
# p.setColor(self.backgroundRole(), QtCore.Qt.red)
# self.setPalette(p)
self.setAttribute(QtCore.Qt.WA_StyledBackground, True)
self.setStyleSheet('background-color: red')

class Window(QtWidgets.QWidget):
def __init__(self):
super(Window, self).__init__()
self.setStyleSheet('background-color: green')
self.widget = AlarmWidget('')
layout = QtWidgets.QVBoxLayout(self)
layout.addWidget(self.widget)

if __name__ == '__main__':

app = QtWidgets.QApplication(sys.argv)
window = Window()
window.setWindowTitle('BG Colour Test')
window.setGeometry(600, 100, 300, 200)
window.show()
sys.exit(app.exec_())

This should show a window containing a red rectangle with a green border, like this:

screenshot

To test things further, set only the palette in the AlarmWidget class, but without setting a stylesheet in the Window class. This should show a red rectangle with no green border. Finally, set only the stylesheet in both classes - but without the setAttribute line. This should show a plain green rectangle with no inner red rectangle (i.e. the stylesheet on the custom widget is no longer applied).

How to set background color to entire widget with stylesheet in PySide

By default, a QWidget does not fill its background. You can either use a QFrame instead or setting the WA_StyledBackground attribute of the QWidget to True as said here : PySide: QWidget does not draw background color.

To apply the style sheet only to the container, and not to its children, the container widget can be named and the style sheet can specifically be applied to it by referring to its name.

Below is a MWE, derived from your code, that shows how it can be done using a QFrame instead of a QWidget :

from PySide import QtGui
import sys

class Widget(QtGui.QWidget):
def __init__(self):
QtGui.QWidget.__init__(self)

mainLayout = QtGui.QVBoxLayout(self)

testWidget = QtGui.QFrame()
testWidget.setFixedSize(100,100)
testWidget.setObjectName("myWidget")
testWidget.setStyleSheet("#myWidget {background-color:red;}")

testLayout = QtGui.QVBoxLayout()

testWidget.setLayout(testLayout)

but = QtGui.QPushButton('TEST')
testLayout.addWidget(but)

mainLayout.addWidget(testWidget)

if __name__ == '__main__':

app = QtGui.QApplication(sys.argv)
instance_1 = Widget()
instance_1.show()
sys.exit(app.exec_())

which results in:

Sample Image

Changing background color of a QWidget is not possible with a css class and setProperty()

If you subclass a custom widget from QWidget, then in order to use the StyleSheets you need to provide a paintEvent to the custom widget. See this page.

So add the following method to your MainWidget class:

def paintEvent(self, event):
"Reimplementation of paintEvent to allow for style sheets"
opt = QtGui.QStyleOption()
opt.initFrom(self)
painter = QtGui.QPainter(self)
self.style().drawPrimitive(QtGui.QStyle.PE_Widget, opt, painter, self)
painter.end()


Related Topics



Leave a reply



Submit