Qt绘制呼吸边框效果

效果图
在这里插入图片描述
头文件

#ifndef QWHBREATHWIDGET_H
#define QWHBREATHWIDGET_H

/*
 * 边框呼吸效果
 * 该控件支持为任意继承自QWidget的窗体设置效果
 * 该控件支持设置边框宽度
 * 该控件支持设置边框颜色
 */

#include <QWidget>
#include <QPainter>
#include <QPaintEvent>
#include <QPropertyAnimation>
#include <QHBoxLayout>

class QWHBreathWidget : public QWidget
{
    Q_OBJECT
    Q_PROPERTY(float m_alpha READ getAlpha WRITE setAlpha)
public:
    explicit QWHBreathWidget(QWidget *parent = nullptr);
    ~QWHBreathWidget();

public:
    //设置特效的窗体
    void setWidget(QWidget *widget);
    //设置边距(边距=线条宽度)
    void setMarginis(int margins);
    //设置边框颜色
    void setBorderColor(QColor color);

    //获取边距
    int getMargins();
    //获取边框颜色
    QColor getBorderColor();

protected:
    void paintEvent(QPaintEvent *);
    void drawBorder(QPainter *painter);

private:
    void setAlpha(float alpha);
    float getAlpha()    const;

private:
    int m_contentMargin;                //保存控件边距
    QColor m_borderColor;               //边框颜色
    float m_alpha;                      //α通道值
    QWidget *m_widget;                  //特效窗体
    QHBoxLayout *m_hLayout;             //水平布局

    QPropertyAnimation *m_animation;    //动画
};

#endif // QWHBREATHWIDGET_H

cpp文件

#include "qwhbreathwidget.h"

QWHBreathWidget::QWHBreathWidget(QWidget *parent) : QWidget(parent)
{
    m_contentMargin = 8;                //保存控件边距
    m_borderColor = Qt::blue;           //边框颜色
    m_alpha = 255;                      //α通道值
    m_widget = nullptr;                 //特效窗体
    m_hLayout = new QHBoxLayout(this);  //水平布局
    m_hLayout->setMargin(0);
    m_hLayout->setContentsMargins(m_contentMargin, m_contentMargin, m_contentMargin, m_contentMargin);
    this->setLayout(m_hLayout);

    m_animation = new QPropertyAnimation(this, "m_alpha", this);
    m_animation->setStartValue(50);
    m_animation->setKeyValueAt(0.5, 255);
    m_animation->setEndValue(50);
    m_animation->setDuration(4000);
    m_animation->setLoopCount(-1);
    m_animation->start();
    connect(m_animation, SIGNAL(finished()), this, SLOT(onFinished()));
}

QWHBreathWidget::~QWHBreathWidget()
{

}

void QWHBreathWidget::setWidget(QWidget *widget)
{
    //清楚布局,但不包括布局内控件
    if (this->layout() != nullptr)
    {
        QLayoutItem *item;
        while ((item = this->layout()->takeAt(0)) != nullptr)
        {
            delete item;
            item = nullptr;
        }
    }
    //重新添加控件
    m_widget = widget;
    m_widget->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
    m_hLayout->addWidget(m_widget);
}

void QWHBreathWidget::setMarginis(int margins)
{
    m_contentMargin = margins;
}

void QWHBreathWidget::setBorderColor(QColor color)
{
    m_borderColor = color;
}

int QWHBreathWidget::getMargins()
{
    return m_contentMargin;
}

QColor QWHBreathWidget::getBorderColor()
{
    return m_borderColor;
}

void QWHBreathWidget::paintEvent(QPaintEvent *)
{
    QPainter painter(this);
    painter.setRenderHint(QPainter::Antialiasing);
    drawBorder(&painter);
}

void QWHBreathWidget::drawBorder(QPainter *painter)
{
    painter->save();

    m_borderColor.setAlpha(m_animation->currentValue().toInt());
    painter->setPen(QPen(m_borderColor, m_contentMargin * 2));
    painter->setBrush(Qt::NoBrush);
    painter->drawRect(rect());

    painter->restore();
}

void QWHBreathWidget::setAlpha(float alpha)
{
    m_alpha = alpha;
    update();
}

float QWHBreathWidget::getAlpha() const
{
    return m_alpha;
}
  • 2
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

浮生卍流年

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值