I am attempting to create a custom QWidget (from PyQt5) whose background colour can change. However, all the standard methods of setting the background colour do not seem to work for a custom QWidget class
So far I have attempted to change the colour through QSS stylesheet and by setting the palette. This works for a regular QWidget but for some reason not a custom widget.
I have found reference custom QWidgets requiring a paintEvent() function in the C++ documentation https://wiki.qt.io/How_to_Change_the_Background_Color_of_QWidget and an did find one reference to it in Python. However, implementing the linked paintevent fails because QStyleOption does not seem to exist in PyQt5.
Below shows a high level of the QWidget class I created (it also contains a bunch of labels) and the QSS I used for the Widget (style has been set in a parent widget but have tried setting it directly)
class AlarmWidget(QWidget):
def __init__(self, alarm, parent=None):
super(AlarmWidget, self).__init__(parent)
self.setFixedHeight(200)
self.setProperty("active", True)
self.setAutoFillBackground(True)
p = self.palette()
p.setColor(self.backgroundRole(), PyQt5.QtCore.Qt.red)
self.setPalette(p)
AlarmWidget {
background-color: red
}
Overall, no matter what I do, it does not let me set the background colour for the custom QWidget so would really appreciate help