Important: Please read the Qt Code of Conduct -

QGraphicsOpacityEffect with an opacity mask shows transparent areas as black

  • Hello everyone :)

    I am creating a QGraphicsOpacityEffect with a QLinearGradient opacity mask and applying the effect to a QGraphicsPixmapItem, as show below;

    QGraphicsPixmapItem* pItem; // please assume pixmap has been properly set

    QGraphicsOpacityEffect* opacity = new QGraphicsOpacityEffect();

    QLinearGradient lg;


    As you can see, I am simply trying to "fade" the edge of the QGraphicsPixmapItem to transparent, however it fades to black, as shown in this picture:


    1. Why is this happening?
    2. How do I make the black areas be transparent?

    Thank you very much!

  • @MadMan27 bump...anyone have any ideas? :)

  • Moderators


    Why is this happening?

    Because your gradient contains a black color.

    How do I make the black areas be transparent?

    What should be there? If you don't want to have black, drop the opacity effect and just paint those areas as transparent.
    For reference look at the painter's composition modes.

    Kind regards.

  • @kshegunov
    Hello :)

    Thank you for answering :) but I think you completely misunderstood my question. The black color does not matter at all, I am just using a black to transparent gradient to create an opacity MASK (which defines which parts of the image should be opaque). I could create the same mask by using ANY other color, it still shows up as black. It is simply an opacity mask.

    Thanks for the answer though! I hope someone knows why Qt is showing areas that SHOULD be transparent as black...
    It seems like the QGraphicsOpacityEffect isn't working properly.

  • For anyone still wondering how to do this, I figured out what the problem, and solution is.

    The problem was that the pixmap of the QGraphicsPixmapItem did not have an alpha channel. So when the QGraphicsOpacityEffect tried to make certain pixels transparent, we saw black instead.

    The solution is simply to add an alpha channel to the pixmap. I am sure there are several ways of doing this, but I simply converted the QPixmap to a QImage, and then traversed each pixel of the image and set the QColor to a color that included alpha values.

    Worked like a charm :)

Log in to reply