Search code examples
qtqpainter

how to change QToolButton opacity using QPainter?


I use QToolButton and according to some conditions I want to change the opacity of my QToolButton or specially icon of it, whether using QPainter or any other way.

How can I do this?


Solution

  • You can either use QGraphicsOpacityEffect on your QToolButton, or you can repaint your icon with different opacity. Use QPainter::CompositionMode_DestinationIn composition mode to reduce the alpha.

    This is a little example of how to use QPainter to do that:

    QImage image(":/img/myimage.png");
    QPainter p;
    p.begin(&image);
    p.setCompositionMode(QPainter::CompositionMode_DestinationIn);
    p.fillRect(image.rect(), QColor(0, 0, 0, 50));
    p.end();