Qt 透明窗体设置

参考自http://blog.sina.com.cn/s/blog_a6fb6cc90101i19x.html

一,全透明

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

 

posted on 2016-12-06 10:36  Just_Boy  阅读(269)  评论(0)    收藏  举报