前言
emm… 懒得写描述了,直接看代码吧,或者先看看最后的效果图也行。
人比较懒,注释也没写,那凑合看好了。有问题请留言交流,看到会及时回复??
聊聊代码
fr_light.h
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 | #ifndef FRLIGHT_H #define FRLIGHT_H #include <qwidget.h> class FrLight : public QWidget { Q_OBJECT public: enum State { kDefault, kNormal, kWorking, kWarning, kMalfunction }; public: explicit FrLight(QWidget *parent = nullptr); void set_state(const State& state); void set_default_color(const QColor& default_color); void set_normal_color(const QColor& normal_color); void set_working_color(const QColor& working_color); void set_warning_color(const QColor& warning_color); void set_malfunction_color(const QColor& malfunction_color); protected: void timerEvent(QTimerEvent *); void paintEvent(QPaintEvent *); private: int _step; int _timer_id; State _state; \ QColor _default_color; QColor _normal_color; QColor _working_color; QColor _warning_color; QColor _malfunction_color; }; #endif // FRLIGHT_H |
fr_light.cc
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 | #include "fr_light.h" #include <qpainter.h> #include <qbrush.h> FrLight::FrLight(QWidget *parent) : QWidget(parent), _step(60) , _timer_id(0) , _state(kWorking) , _default_color(127, 127, 127) , _normal_color(32, 255, 32) , _working_color(32, 255, 32) , _warning_color(255, 255, 32) , _malfunction_color(255, 32, 32) { update(); } void FrLight::set_state(const FrLight::State& state) { _state = state; if (kWorking == _state) { _timer_id = startTimer(16); } else { killTimer(_timer_id); _timer_id = 0; } update(); } void FrLight::set_default_color(const QColor& default_color) { _default_color = default_color; update(); } void FrLight::set_normal_color(const QColor& normal_color) { _normal_color = normal_color; update(); } void FrLight::set_working_color(const QColor& working_color) { _working_color = working_color; update(); } void FrLight::set_warning_color(const QColor& warning_color) { _warning_color = warning_color; update(); } void FrLight::set_malfunction_color(const QColor& malfunction_color) { _malfunction_color = malfunction_color; update(); } void FrLight::timerEvent(QTimerEvent*) { update(); --_step; if (_step == -60) { _step = 60; } } void FrLight::paintEvent(QPaintEvent*) { int side = qMin(width(), height()); QPainter painter(this); painter.setRenderHint(QPainter::Antialiasing, true); painter.translate(width() / 2, height() / 2); painter.scale(side / 100.0, side / 100.0); QColor start_color, mid_color, end_color; switch (_state) { case kDefault: start_color = _default_color; mid_color = _default_color; end_color = _default_color; mid_color.setAlpha(192); end_color.setAlpha(0); break; case kNormal: start_color = _normal_color; mid_color = _normal_color; end_color = _normal_color; mid_color.setAlpha(192); end_color.setAlpha(0); break; case kWorking: if (0 == _timer_id) { _timer_id = startTimer(16); } start_color = _working_color; mid_color = _working_color; end_color = _working_color; start_color.setAlpha(qAbs(int(_step * 4.25))); mid_color.setAlpha(qAbs(int(_step * 3.2))); end_color.setAlpha(0); break; case kWarning: start_color = _warning_color; mid_color = _warning_color; end_color = _warning_color; mid_color.setAlpha(192); end_color.setAlpha(0); break; case kMalfunction: start_color = _malfunction_color; mid_color = _malfunction_color; end_color = _malfunction_color; mid_color.setAlpha(192); end_color.setAlpha(0); break; default: start_color = _default_color; mid_color = _default_color; end_color = _default_color; mid_color.setAlpha(192); end_color.setAlpha(0); break; } QRadialGradient radial(0, 0, 50, 0, 0); //设置圆的原点和焦点在中心,半径50 radial.setSpread(QGradient::PadSpread); radial.setColorAt(0, start_color); radial.setColorAt(0.9, mid_color); radial.setColorAt(1, end_color); painter.setPen(Qt::transparent); painter.setBrush(radial); painter.drawEllipse(-50, -50, 100, 100); } |