Monday, April 5, 2010

Transparent QImage or QPixmap picture

To create transparent picture use QPainter's method setCompositionMode and QPixmap filled with transparent color.

Code example:

QImage image("123.png");

// Create new picture for transparent
QPixmap transparent(image.size());
// Do transparency
transparent.fill(Qt::transparent);
QPainter p;
p.begin(&transparent);
p.setCompositionMode(QPainter::CompositionMode_Source);
p.drawPixmap(0, 0, QPixmap::fromImage(image));
p.setCompositionMode(QPainter::CompositionMode_DestinationIn);

// Set transparency level to 150 (possible values are 0-255)
// The alpha channel of a color specifies the transparency effect, 
// 0 represents a fully transparent color, while 255 represents
// a fully opaque color.
p.fillRect(transparent.rect(), QColor(0, 0, 0, 150));
p.end();

//put semi-transparent picture to QLabel 
QLabel* label = new QLabel(); 
label->setPixmap(transparent);

4 comments: