這個控件是好早之前寫的,已經受權過好幾我的開源過此控件代碼,好比紅磨坊小胖,此控件並非來源於真實需求,而僅僅是突發奇想,相似於星星的閃爍,越到邊緣愈來愈淡,定時器動態改變邊緣發光的亮度,產生呼吸的效果,別名叫會呼吸的痛,看到這個歌名,又讓我想起了前女朋友,哎!久久不能忘懷! 大體的原理就是使用了錐形漸變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(); } }