Question

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!

Was it helpful?

Solution

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.setBrush(b);
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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top