qt5-透明

 方法一:窗体和子窗体都透明

    this->resize(400,300);
    QLabel* label=new QLabel("标签",this);
    label->move(10,10);
    label->resize(100,100);
    label->setStyleSheet("background-color: rgb(255, 251, 100)");

    this->setWindowOpacity(0);//设置部件的透明度
    //其范围是 1.0(不透明)到 0.0(透明),默认值为 1.0
    //窗体及其子窗体全部透明

 

方法二--推荐--主窗体透明(子窗体不透明)

#include "win.h"

Win::Win(QWidget *parent)
    : QWidget(parent)
{

    this->resize(400,300);
    QLabel* label=new QLabel("标签",this);
    label->move(10,10);
    label->resize(100,100);
    label->setStyleSheet("background-color: rgb(255, 251, 100)");

    this->setWindowFlags(Qt::FramelessWindowHint|Qt::WindowMinimizeButtonHint);
    //注意:如果不去除标题栏,透明部分是黑色
    this->setAttribute(Qt::WA_TranslucentBackground, true);  //是否透明


}

Win::~Win()
{
}

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

 

方法三-主窗体透明(子窗体不透明) --实现不规则窗体

#include "win.h"

Win::Win(QWidget *parent)
    : QWidget(parent)
{

    this->setWindowFlags(Qt::FramelessWindowHint|Qt::WindowMinimizeButtonHint);
    //注意:如果不去除标题栏,透明部分是黑色
    this->setAttribute(Qt::WA_TranslucentBackground, true);  //是否透明
    p=new QPixmap("./b.png");
    //b.png带透明的图片
    this->resize(p->size());

}

Win::~Win()
{
}

void Win::paintEvent(QPaintEvent *)
{


    QPainter painter(this);
    painter.drawPixmap(0,0,*p);

}

上面工程下载:链接:https://pan.baidu.com/s/1MWLxvDTt-z4bEVmeNjC2rg    提取码:6666   

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

posted @ 2019-08-25 14:52  天子骄龙  阅读(268)  评论(0编辑  收藏  举报