Take the 2-minute tour ×

I am fairly new to Qt and have been doing a lot of reading and practicing exercises. I have searched for this one quite a lot but I could not find any examples.

I have a QPixmap object to which I load a .png image. I need to create a copy of this QPixmap with a dark mask applied to it.

Basically I want this QPixmap's image to be covered with a layer of solid black of which the opacity is set to 50%.

I know how to set the opacity of a QPixmap's image but how can I add a layer of solid black with opacity on it?

Thank you!

share|improve this question
add comment

1 Answer

up vote 2 down vote accepted

You can use a QPainter and you a semi-transparent QBrush to paint that dark layer onto your QPixmap.

Assuming pic is a QPixmap loaded with your image:

QPainter p(&pic);
QBrush b(QColor(0,0,0,128)); // adjust color and alpha to taste
p.drawRect(0, 0, 200, 200);

Effect (before/after):

enter image description here    vs    enter image description here

The opaque black border can be removed by setting an semi-transparent pen before painting.
Copy the pixmap before applying the "mask" if you want to preserve the original.

share|improve this answer
Thank you Mat, this is excellent. –  Phil Dec 29 '12 at 19:14
add comment

Your Answer


