使 QLabel 的像素图透明 [英] Make a pixmap transparent for a QLabel

查看:47
本文介绍了使 QLabel 的像素图透明的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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?

感谢您抽出宝贵时间.

推荐答案

如果您的图像不是按原样透明而您希望它是透明的,您可以执行以下操作:

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));

这篇关于使 QLabel 的像素图透明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆