5

I have a MainWindow with a QLabel and a pixmap. I want to make it transparent (or less opaque)

I am using the following code below.

ui->label->setAttribute(Qt::WA_TranslucentBackground);
ui->label->repaint();

However it does not seem to work. The image looks the same without any changes. I also tried to use to the following statement:

    ui->label->setStyleSheet("background-color: rgba(255, 255, 255, 10);");

Unfortunately, this does not seem to work either.

Anyone knows how can I make an image transparent or make it less opaque?

Thank you for your time.

László Papp
  • 51,870
  • 39
  • 111
  • 135
Elseine
  • 741
  • 2
  • 11
  • 23

3 Answers3

7

If your image isn't transparent as it is and you want it to be, you can do something like this:

QLabel *l = new QLabel(this);
QImage image(":/img/myimage.png");
QPainter p;
p.begin(&image);
p.setCompositionMode(QPainter::CompositionMode_DestinationIn);
p.fillRect(image.rect(), QColor(0, 0, 0, 50));
p.end();
l->setPixmap(QPixmap::fromImage(image));
thuga
  • 12,601
  • 42
  • 52
2

You can use method fill, example:

pixmap = QPixmap(width_size_in_pixels, height_size_in_pixels)
pixmap.fill(Qt.transparent)
antipups
  • 153
  • 3
  • 9
0

You can apply QGraphicsOpacityEffect to a label to adjust it's opacity.

Dmitry Sazonov
  • 8,801
  • 1
  • 35
  • 61