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: