窗体部件效果之阴影

Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);

    this->setAttribute(Qt::WA_TranslucentBackground);
    this->setWindowFlags(Qt::FramelessWindowHint | Qt::WindowStaysOnTopHint |Qt::CustomizeWindowHint);

    QGraphicsDropShadowEffect *shadow_effect = new QGraphicsDropShadowEffect(this);
    shadow_effect->setOffset(10, 10);
    shadow_effect->setColor(Qt::gray);
    shadow_effect->setBlurRadius(8);

//    //1. 控件阴影效果
//    ui->pushButton->setGraphicsEffect(shadow_effect);//不能共用一个阴影效果,只有最后一个设置的有效
//    ui->radioButton->setGraphicsEffect(shadow_effect);
//    ui->checkBox->setGraphicsEffect(shadow_effect);
//    ui->comboBox->setGraphicsEffect(shadow_effect);
//    ui->spinBox->setGraphicsEffect(shadow_effect);
//    ui->lineEdit->setGraphicsEffect(shadow_effect);

//    //2. 窗口阴影效果(这种方法有问题,还是要重写paintevent)
//    setGraphicsEffect(shadow_effect);

}

Widget::~Widget()
{
    delete ui;
}

//2. 窗口阴影效果
void Widget::paintEvent(QPaintEvent *event)
{
    QPainterPath path;
    path.setFillRule(Qt::WindingFill);
    path.addRect(10, 10, width()-20, height()-20);
    QPainter painter(this);
    painter.setRenderHint(QPainter::Antialiasing, true);
    painter.fillPath(path, QBrush(Qt::white));

    QColor color(0, 0, 50);
    for(int i = 0; i < 10; i++)
    {
        QPainterPath path;
        path.setFillRule(Qt::WindingFill);
        path.addRect(10-i, 10-i, width()-(10-i)*2, height()-(10-i)*2);
        color.setAlpha(150 - sqrt(i)*50);
        painter.setPen(color);
        painter.drawPath(path);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值