我有一个在Qt中覆盖在视频上的透明图像(QImage)。我希望仅在按钮点击时更改透明图像的颜色。请问如何实现?
谢谢。
谢谢。
可以用多种方式实现。我建议使用QPainter
创建新图像。如果将SourceIn
合成模式设置为起始图像的alpha通道,那么您所绘制的任何图形都会应用该通道。您只需要用所需的颜色填充图像即可。
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
请注意,我使用QPixmap
而不是QImage
,因为QPixmap
更有效率地显示(以及可能绘制)。如果您出于某些原因仍然想使用QImage
,则此代码将在不做任何更改的情况下(当然,最后一行除外)与QImage
一起工作。
源图像:
结果:
source_image = source_image.convertToFormat(QImage::Format_ARGB32);
将图像转换为可绘制格式。 - Pavel Strakhovsource_image.convertToFormat(QImage::Format_ARGB32);
。我使用painter.fillRect(new_image.rect(), QColor("red");
代替base_color
。但是我得到了一个空标签。 - SexyBeast