I have a shape (in blue) loaded from a PNG with transparency:

Then I’m drawing several circles on top of this shape (in red) with QPainter::drawEllipse.

The result of that is somewhat similar to the third picture with the red shape completely covering the blue one:

What I would like however is for the blue shape to act as a mask to the red one, with a result like this:

Is it possible to do that with QPainter?
It’s possible. Assuming you’re loading your PNG into a QImage, you can do something like this to create a mask from your image:
See the other to
create*Maskfunctions in QImage for alternatives.Then it’s a simple matter of setting the painter’s clip region:
Here’s a stupid demo (don’t use that code as-is, the image loading, mask and region creation should be cached, they are potentially expensive):
Which produces something like this:
