Question

I would like to make a Subclass of QLabel that acts as a boolean toggle button. I would like to be able to assign any pixmap, and have the image be desaturated when the button state is False. I am guessing this could be done with QColor somehow, but am not really sure how I would implement this. Any Ideas?

Was it helpful?

Solution

You should be able to do what you want using QGraphicsColorizeEffect:

    self.label = QtGui.QLabel(self)
    effect = QtGui.QGraphicsColorizeEffect(self.label)
    effect.setStrength(0.0)
    effect.setColor(QtGui.QColor('silver'))
    self.label.setGraphicsEffect(effect)

To toggle the effect, set the strength as appropriate:

    if self.label.graphicsEffect().strength():
        self.label.graphicsEffect().setStrength(0.0)
    else:
        self.label.graphicsEffect().setStrength(0.5)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top