Reputation: 703
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.
Upvotes: 3
Views: 9261
Reputation: 40512
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 QPixmap
s 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:
Result:
Upvotes: 11