Qt如何设置界面透明

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、设置子控件透明:
可以在Qt Designer中,直接右键改变样式表,来设置其背景色:

background-color: rgba(0, 0, 0, 0);

也可以在程序中添加代码:

setAttribute(Qt::WA_TranslucentBackground, true);

3、窗口及其上面的控件都半透明:

setWindowOpacity(0.7)//取值范围为:0.0 - 1.0,默认值为1.0,全透明为0.0,不透明则为1.0。

4、主窗体采用背景图片(百度的)

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

你可能感兴趣的:(Qt学习笔记)