设置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));
}
完成!!!