Qt之窗体透明 (三种不同的方法和效果)

一、全透明
setWindowOpacity(0.5);
取值范围为:0.0 - 1.0,默认值为1.0,全透明为0.0,不透明则为1.0。
二、主窗体透明(子窗体不透明)
1、主窗体采用背景色

setAttribute(Qt::WA_TranslucentBackground, true);
void paintEvent(QPaintEvent *event)
{
    QPainter painter(this);
    painter.fillRect(this->rect(), QColor(0, 0, 255, 80));  //QColor最后一个参数80代表背景的透明度
}

2、主窗体采用背景图片

setAttribute(Qt::WA_TranslucentBackground, true);
void QZXingWidget::paintEvent(QPaintEvent *event)
{
    QPixmap covert_pixmap(":/Images/background");
    QPixmap pixmap(covert_pixmap.width(), covert_pixmap.height());
    pixmap.fill(Qt::transparent); 
    QPainter painter(&pixmap);
    QPoint start_point(0, 0);
    QPoint end_point(0, pixmap.height());
    //QLinearGradient进行渐变色设置
    QLinearGradient linear_gradient(start_point, end_point);
    linear_gradient.setColorAt(0, QColor(255, 255, 255, 100));
    linear_gradient.setColorAt(0.5, QColor(255, 255, 255, 150));
    linear_gradient.setColorAt(1, QColor(255, 255, 255, 255));
    painter.fillRect(this->rect(), QBrush(linear_gradient));
    painter.setCompositionMode(QPainter::CompositionMode_SourceIn); 
    painter.drawPixmap(0, 0, covert_pixmap);
    painter.end();
 
    QPainter painter2(this);
    painter2.drawPixmap(0, 0, pixmap);
}

三、子窗体透明(主窗体不透明)
这部分我就不过多阐述了,请参考:http://blog.sina.com.cn/s/blog_a6fb6cc90101az3h.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值