为QLabel制作透明像素图

Els*_*ine 3 qt transparency qpixmap

我有一个带有QLabel和pixmap的MainWindow.我想让它透明(或不透明)

我在下面使用以下代码.

ui->label->setAttribute(Qt::WA_TranslucentBackground);
ui->label->repaint();
Run Code Online (Sandbox Code Playgroud)

但它似乎不起作用.图像看起来没有任何变化.我也尝试使用以下声明:

    ui->label->setStyleSheet("background-color: rgba(255, 255, 255, 10);");
Run Code Online (Sandbox Code Playgroud)

不幸的是,这似乎也不起作用.

任何人都知道如何使图像透明或使其不透明?

感谢您的时间.

thu*_*uga 5

如果您的图像不是透明的并且您希望它是这样的,您可以执行以下操作:

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));
Run Code Online (Sandbox Code Playgroud)