Search code examples
c++qtuser-interfaceiconsqpainter

Qt: creating semi-transparent disabled icon states


I want to dinamically create a semi-transparent icon Pixmap for my icons disabled states, from the original icon images (without using extra images for the disabled states).

I thought this would take five minutes, just create a QPainter, set it's opacity to 0.5 or something, and draw the normal Pixmap into it.

The problem is that the QPainter seems to start with a background set to (205, 205, 205), and there's nothing I can throw at it to make it fully-transparent.

This is the code I'm using for the standard icons:

icon.addPixmap(QPixmap(filename));

This is what I've tried so far to make a transparent version of it for disabled state:

QPixmap normalPixmap(filename);
QPixmap disabledPixmap(normalPixmap.size());
QPainter p(&disabledPixmap);

p.setBackgroundMode(Qt::TransparentMode);
p.setBackground(QBrush(Qt::transparent));
p.eraseRect(normalPixmap.rect());
// (...) I've tried Composition modes and a lot of other stuff here, with no success

p.setOpacity(0.5);
p.drawPixmap(0, 0, normalPixmap);

p.end();
icon.addPixmap(disabledPixmap, QIcon::Disabled, QIcon::On);

These are the results I get from the above codes:

enter image description here


Solution

  • try:

    disabledPixmap.fill(Qt::transparent);

    before creating the QPainter