4.7 and like to overlay two images on a qgraphicsview. The image on top shall be semi-transparent to allow to see through it. Initially both images are fully opaque. I expected some function for setting a global alpha-value for each pixel to exist, but it seems like there is no such function. The closest thing to it is QPixmap::setAlphaChannel(const QPixmap & alphaChannel), which, however, is marked as obsolete since Qt-4.6. Instead the manual refers to the CompositionModes of QPainter, but I don't succeed to add transparency to an opaque image like I want. Could anyone point me to a working example or share some code?
Edit: I'm almost sorry for having an own answer, now just a few hours after asking the question. From this article I figured out that the following code does the job. I just wonder if this is considered "better" (which often translates to faster) than modifying the alpha values pixelwise.
QPainter p;
p.begin(image);
p.setCompositionMode(QPainter::CompositionMode_DestinationIn);
p.fillRect(image->rect(), QColor(0, 0, 0, 120));
p.end();
mpGraphicsView->scene()->addPixmap(QPixmap::fromImage(image->mirrored(false,true),0));
Use your painter object and set the opacity.
void ClassName::paintEvent(QPaintEvent *event)
{
QPainter painter(this);
painter.setOpacity(1.00); //0.00 = 0%, 1.00 = 100% opacity.
painter.drawPixmap(QPixmap(path));
}