效果图
头文件
#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;
}