I'm trying to subclass a QCheckBox
to inherit its behavior, but subclassing its paintEvent
so that I can draw it like Android's toggle button. Is there a way to allow its stylesheet to determine the color it draws with?
In the example below I'm drawing an ellipse with a color I made as a qt Property
in the hopes that I could do something like inst.setStyleSheet("MyCheckBox {MyColor: red;}")
to make it red, but it does nothing. Is this possible?
from PySide2 import QtCore
from PySide2 import QtGui
from PySide2 import QtWidgets
class MyCheckBox(QtWidgets.QCheckBox):
def __init__(self, parent=None):
super(MyCheckBox, self).__init__(parent)
self._myColor = QtGui.QColor(0, 255, 0)
def myColor(self):
return self._myColor
def setMyColor(self, qcolor):
self._myColor = qcolor
MyColor = QtCore.Property(QtGui.QColor, myColor, setMyColor)
def paintEvent(self, event):
paint = QtGui.QPainter(self)
paint.setBrush(self._myColor)
paint.drawEllipse(0, 0, self.width(), self.height())
As the docs points out the syntax is qproperty-<property name>
.
MyCheckBox {qproperty-MyColor: red;}