这个控件是好早之前写的,已经受权过好几我的开源过此控件代码,好比红磨坊小胖,此控件并非来源于真实需求,而仅仅是突发奇想,相似于星星的闪烁,越到边缘愈来愈淡,定时器动态改变边缘发光的亮度,产生呼吸的效果,别名叫会呼吸的痛,看到这个歌名,又让我想起了前女朋友,哎!久久不能忘怀! 大体的原理就是使用了锥形渐变QRadialGradient,而后定时器改变该渐变画刷的颜色的透明度值,产生呼吸效果。Qt中提供了好多种渐变画刷,很是有用,能够执行画刷的区域,而后等比例插值,指定插值对应的颜色,这样使用起来就很是的丰富了。linux
#ifndef LIGHTPOINT_H #define LIGHTPOINT_H /** * 呼吸点控件 做者:feiyangqingyun(QQ:517216493) 2017-11-27 * 1:可设置呼吸间隔 * 2:可设置颜色透明渐变步长 * 3:可设置背景颜色 */ #include <QWidget> #ifdef quc #if (QT_VERSION < QT_VERSION_CHECK(5,7,0)) #include <QtDesigner/QDesignerExportWidget> #else #include <QtUiPlugin/QDesignerExportWidget> #endif class QDESIGNER_WIDGET_EXPORT LightPoint : public QWidget #else class LightPoint : public QWidget #endif { Q_OBJECT Q_PROPERTY(int step READ getStep WRITE setStep) Q_PROPERTY(int interval READ getInterval WRITE setInterval) Q_PROPERTY(QColor bgColor READ getBgColor WRITE setBgColor) public: explicit LightPoint(QWidget *parent = 0); ~LightPoint(); protected: void paintEvent(QPaintEvent *); void drawBg(QPainter *painter); private: int step; //颜色透明渐变步长 int interval; //定时器间隔 QColor bgColor; //背景颜色 QTimer *timer; //绘制定时器 int offset; //偏移量 bool add; //是否增长 public: int getStep() const; int getInterval() const; QColor getBgColor() const; QSize sizeHint() const; QSize minimumSizeHint() const; public slots: //设置颜色透明渐变步长 void setStep(int step); //设置定时器间隔 void setInterval(int interval); //设置背景颜色 void setBgColor(const QColor &bgColor); }; #endif // LIGHTPOINT_H
#pragma execution_character_set("utf-8") #include "lightpoint.h" #include "qpainter.h" #include "qevent.h" #include "qtimer.h" #include "qdebug.h" LightPoint::LightPoint(QWidget *parent) : QWidget(parent) { step = 10; interval = 100; bgColor = QColor(255, 0, 0); timer = new QTimer(this); connect(timer, SIGNAL(timeout()), this, SLOT(update())); timer->start(100); offset = 0; add = true; } LightPoint::~LightPoint() { if (timer->isActive()) { timer->stop(); } } void LightPoint::paintEvent(QPaintEvent *) { int width = this->width(); int height = this->height(); int side = qMin(width, height); //绘制准备工做,启用反锯齿,平移坐标轴中心,等比例缩放 QPainter painter(this); painter.setRenderHints(QPainter::Antialiasing | QPainter::TextAntialiasing); painter.translate(width / 2, height / 2); painter.scale(side / 200.0, side / 200.0); //绘制背景 drawBg(&painter); } void LightPoint::drawBg(QPainter *painter) { int radius = 99; painter->save(); QRadialGradient g(QPoint(0, 0), radius); (offset < 70 && add) ? (offset += step) : (add = false); (offset > 0 && !add) ? (offset -= step) : (add = true); bgColor.setAlpha(255); g.setColorAt(0.1, bgColor); bgColor.setAlpha(100 + offset); g.setColorAt(0.3, bgColor); bgColor.setAlpha(50 + offset); g.setColorAt(0.6, bgColor); bgColor.setAlpha(0); g.setColorAt(1.0, bgColor); painter->setPen(Qt::NoPen); painter->setBrush(g); painter->drawEllipse(-radius, -radius, radius * 2, radius * 2); painter->restore(); } int LightPoint::getStep() const { return this->step; } int LightPoint::getInterval() const { return this->interval; } QColor LightPoint::getBgColor() const { return this->bgColor; } QSize LightPoint::sizeHint() const { return QSize(100, 100); } QSize LightPoint::minimumSizeHint() const { return QSize(5, 5); } void LightPoint::setStep(int step) { if (this->step != step) { this->step = step; update(); } } void LightPoint::setInterval(int interval) { if (this->interval != interval) { this->interval = interval; timer->setInterval(interval); update(); } } void LightPoint::setBgColor(const QColor &bgColor) { if (this->bgColor != bgColor) { this->bgColor = bgColor; update(); } }