一、在qss中设置
1.要先在qt.cpp中加入
//窗口透明
setAttribute(Qt::WA_TranslucentBackground, true);
2.然后qss中使用线性渐变
border-top: 1px solid QLinearGradient(y0:0, y1:1,stop: 0 #DADADA, stop: 1 transparent);
border-left: 1px solid QLinearGradient(x0:0, x1:1,stop: 0 #DADADA, stop: 1 transparent);
border-bottom: 1px solid QLinearGradient(y0:0, y1:1,stop: 0 transparent, stop: 1 #DADADA);
border-right: 1px solid QLinearGradient(x0:0, x1:1,stop: 0 transparent, stop: 1 #DADADA);
(不设置窗口透明时,背景是白底。加入后有莫名其妙的黑底...暂时未解决,所以使用方法2)
二、在Qt中设置
1. *.h中加入
public:
void paintEvent(QPaintEvent *event);
2. *.cpp中加入头文件
#include <QtCore/qmath.h> //qSqrt需要
#include <QPaintEvent>
#include <QPainter>
#include <QRectF>
3. *.cpp中加入
//窗口透明
setAttribute(Qt::WA_TranslucentBackground, true);
//去边框,自定义边框
Qt::WindowFlags flags = Qt::Dialog;
flags |= Qt::FramelessWindowHint;
setWindowFlags(flags);
4. *.cpp中加入
可用,好用。
我提前在UI中设置将四边边距设置成8
void TipsDlg::paintEvent(QPaintEvent *event)
{
QPainterPath path;
QRectF rect;
path.setFillRule(Qt::WindingFill);
path.addRect(10, 10, this->width()-20, this->height()-20); //矩形坐标
path.addRoundRect(rect, 8, 8); //矩形圆角
QPainter painter(this);
painter.setRenderHint(QPainter::Antialiasing, true);
painter.fillPath(path, QBrush(Qt::white));
QColor color(0, 0, 0, 50); //rgb和透明度
for(int i=0; i<10; i++)
{
QPainterPath path;
path.setFillRule(Qt::WindingFill);
path.addRect(10-i, 10-i, this->width()-(10-i)*2, this->height()-(10-i)*2);
//透明度渐变,改变*50可以改变透明速度
color.setAlpha(150 - qSqrt(i)*50);
painter.setPen(color);
painter.drawPath(path);
}
}
三、老大给了一个方法,但是不适用于qss。似乎因为qss只适用于css2.0以下版本,但这个box-shadow是css3.0里的...
QDialog#MyDialog
{
background: #FFFFFF;
border: 1px solid #CCCCCC;
box-shadow: 0 1px 3px 0 rgba(0, 0, 0, 0.20);
}
四、
// 设置阴影边框;
auto shadowEffect = new QGraphicsDropShadowEffect(this);
shadowEffect->setOffset(0, 0);
shadowEffect->setColor(Qt::gray);
shadowEffect->setBlurRadius(SHADOW_WIDTH);
this->setGraphicsEffect(shadowEffect);