Question

I have a transparent image (QImage) overlayed over a video in Qt. I want to change the color of the transparent image only on clicks of button. Can some one tell me how to do this?

Thank You.

Was it helpful?

Solution

This can be done in many ways. I suggest to use QPainter to create new image. If you set SourceIn composition mode, starting image's alpha channel will be applied to any drawing that you will do. You just need to fill image with desired color.

QPixmap source_image; // should be preserved in a class member variable
QRgb base_color; // desired image color

QPixmap new_image = source_image;
QPainter painter(&new_image);
painter.setCompositionMode(QPainter::CompositionMode_SourceIn);
painter.fillRect(new_image.rect(), base_color);
painter.end();

ui->label->setPixmap(new_image); // showing result

Note that I use QPixmap instead of QImage because QPixmaps are more efficient to display (and possibly paint). If you for some reason still want to use QImage, this code will work with QImage without any changes (excluding the last line of course).

Source image: source image        Result: result

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