2

I have a very small Qt application that uses labels to display a jpeg image without first putting it in a window. (I got a lot of help from Display QImage with QtGui)

Now I would like to change the alpha channel of this jpeg to make the image partially transparent. I have tried the following without any luck

int main (int argc, char *argv[])
{
    QApplication app(argc, argv);
    QLabel label (0, Qt::FramelessWindowHint | Qt::WindowStaysOnTopHint);
    label.resize(1280,720);
    label.setPixmap(QPixmap("test.jpg"));
    label.setScaledContents(true);

    // This line should set the alpha transparency to 50%
    label.setStyleSheet("background-color: rgba(255,255,255,50);");

    label.show();
    return app.exec();
}

It seems like the Style Sheet isn't affecting the label at all. I have experimented with changing the other rgb values (all 0's for instance) and alternated between the background-color and the color, but the image is always the same.

Update: Thanks to eyllanesc, the following now works for me:

int main (int argc, char *argv[])
 {
     QApplication app(argc, argv);

     QPixmap input ("test.jpg");
     QImage image(input.size(), QImage::Format_ARGB32_Premultiplied);
     image.fill(Qt::transparent);
     QPainter p(&image);
     p.setOpacity(0.5);
     p.drawPixmap(0,0,input);
     p.end();

     QPixmap output = QPixmap::fromImage(image);

     QLabel label (0, Qt::FramelessWindowHint | Qt::WindowStaysOnTopHint);
     label.setStyleSheet("background-color: rgba(255,255,255,50);");
     label.resize(1280,720);
     label.setPixmap(output);
     label.setScaledContents(true);
     label.show();

     return app.exec();
 }
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
Josh Kurland
  • 197
  • 1
  • 3
  • 12
  • Are you trying to make the image background slightly transparent while leaving the rest of the image fully opaque? Or are you trying to make the entire jpg transparent? I don't believe that jpgs support alpha channels so you can't have partially transparent jpg images, but you can make the entire image transparent using either custom rendering or a QGraphicsOpacityEffect graphics effect. – aatwo May 11 '17 at 18:43
  • I need to make the entire image transparent. My goal would be to have a still picture (jpeg, png, etc) as an overlay for some video. – Josh Kurland May 11 '17 at 20:20

1 Answers1

2

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);
eyllanesc
  • 235,170
  • 19
  • 170
  • 241