非常感谢4楼,我试过了,代码如下,没看到预期的效果,哪位还有其他办法????????????
PicTrans::PicTrans(QWidget *parent,const char * name)
: QWidget(parent)
{
pixmap=QPixmap(QString::fromUtf8(name));
bglabel=new QLabel(this);
bglabel->setAutoFillBackground (true);
bglabel->setScaledContents(true);
QPalette palette ;
palette.setBrush(QPalette::Background,QBrush(QPixmap(pixmap)));
palette.setColor(QPalette::WindowText,Qt::white);
bglabel->setPalette(palette);
QVBoxLayout *layout = new QVBoxLayout;
layout->addWidget(bglabel);
setLayout(layout);
}
void PicTrans::paintEvent(QPaintEvent * e)
{
QPixmap temp(pixmap.size());
temp.fill(Qt::transparent);
QPainter p(&temp);
p.setCompositionMode(QPainter::CompositionMode_Source);
p.drawPixmap(0,0,pixmap);
p.setCompositionMode(QPainter::CompositionMode_Destination);
p.fillRect(temp.rect(),QColor(0,0,0,opacity));
p.end();
pixmap=temp;
QPalette palette ;
palette.setBrush(QPalette::Background,QBrush(QPixmap(pixmap)));
bglabel->setPalette(palette);
}
void PicTrans::SetTransValue(int value)
{
if(value>=0&&value<=255)
opacity=value;
repaint();
}