设置QT的窗体、子窗体背景透明

设置QT的窗体、子窗体背景透明度以及不同控件有不同透明度

效果非常好看:

相关代码:

主窗体构造函数主要创建了这两个窗体

    pa = new sonA(this);
    pa->show();

    m_pwidget = new widget(this);
    m_pwidget->show();
    setStyleSheet("QWidget#frame11{border-image: url(:/mouseTrack/Resources/bj.png);}");

两个子窗体:

子窗体1:

构造函数代码

ui.frame->resize(width(), height());

    setWindowFlags(Qt::FramelessWindowHint);
    setAttribute(Qt::WA_TranslucentBackground);

    ui.tableWidget->setStyleSheet("QTableView{background:transparent;border:1px solid #A7 A7 A7;border-radius:1px;font-size:16px;font-family:'黑体';gridline-color:#A7 A7 A7}"\
        "QTableWidget QTableCornerButton::section{background:transparent;}"\
        "QHeaderView{background:rgb(68,83,101);border-radius:1px;height:35;font-size:16px;font-family:'黑体'}"\
        "QHeaderView::section{background:rgb(68,83,101);padding:5px 10px 5px 10px;border-radius:1px;}"\
        "QTableView:item{background:transparent;color:#00FF7F;}"\
        "QTableView:indicator{width:20px;height:20px;}");

    QDesktopWidget *destop = new QDesktopWidget;
    QRect destopRect = destop->screenGeometry();
    move(0, 30);
    m_areaMovable = ui.frame->geometry();

//重载绘制事件

void widget::paintEvent(QPaintEvent *e)
{
    QPainter p(this);
    p.fillRect(rect(), QColor(192, 253, 123, 100));

}

第二个子窗体构造函数部分:

ui.frame->resize(width(), height());
    this->setWindowFlags(Qt::FramelessWindowHint);
    
    setAttribute(Qt::WA_TranslucentBackground, true);
    QColor color("#ADD8E6");
    int r = color.red();
    int g = color.green();
    int b = color.blue();
    QString strStyleSheet = QString("QListWidget#listWidget{background: rgba(151, 224, 255,200);}"\
        "QLineEdit#lineEdit{background: rgba(%1,%2,%3,200);color:#00FF7F;}").arg(r).arg(g).arg(b);
    setStyleSheet(strStyleSheet);
    QDesktopWidget *destop = new QDesktopWidget;

    QRect destopRect = destop->screenGeometry();
    move(destopRect.width() - width(), 30);
    m_areaMovable = ui.frame->geometry();

重写绘制事件:

void sonA::paintEvent(QPaintEvent *e)
{
    QPainter p(this);
    p.fillRect(rect(), QColor(192, 253, 123, 100));

}

完成!!!

  • 4
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值