Modify alpha channel transparency of a windowless QLabel

坚强是说给别人听的谎言 提交于 2019-12-02 02:27:40

The StyleSheet is working fine, the problem is that the QPixmap object is drawn on the background (not the background). If you want QPixmap to be transparent you can use one of two methods:

  1. First Method:

QPixmap input("test.jpg");

QImage image(input.size(), QImage::Format_ARGB32_Premultiplied);
image.fill(Qt::transparent);
QPainter p(&image);
p.setOpacity(0.2);
p.drawPixmap(0, 0, input);
p.end();

QPixmap output = QPixmap::fromImage(image);
label.setPixmap(output);
  1. Second Method:

QPixmap input("test.jpg");
QPixmap output(input.size());
output.fill(Qt::transparent);
QPainter p(&output);
p.setOpacity(0.2);
p.drawPixmap(0, 0, input);
p.end();

label.setPixmap(output);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!