菊花圆圈转动动画
CustomProgressIndicator.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 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
| #ifndef CUSTOMPROGRESSINDICATOR_H #define CUSTOMPROGRESSINDICATOR_H #include <QWidget> #include <QColor>
class CustomProgressIndicator : public QWidget { Q_OBJECT Q_PROPERTY(int delay READ animationDelay WRITE setAnimationDelay) Q_PROPERTY(bool displayedWhenStopped READ isDisplayedWhenStopped WRITE setDisplayedWhenStopped) Q_PROPERTY(QColor color READ color WRITE setColor) public: CustomProgressIndicator(QWidget* parent = 0); int animationDelay() const { return delay_; } bool isAnimated () const; bool isDisplayedWhenStopped() const; const QColor & color() const { return color_; } virtual QSize sizeHint() const; void setBackground(const QString& _icon) { currentPix_ = QPixmap(_icon); } signals: void Finished(void); public slots: void startAnimation(); void stopAnimation(); void setAnimationDelay(int delay); void setDisplayedWhenStopped(bool state); void setColor(const QColor & color);
void onProgress(short _progress) { progress_ = _progress; } protected: virtual void timerEvent(QTimerEvent * event); virtual void paintEvent(QPaintEvent * event); private: unsigned int angle_; int timerId_; int delay_; bool displayedWhenStopped_; QColor color_; short progress_; QPixmap currentPix_; }; #endif
|
CustomProgressIndicator.cpp
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
| #include "customprogressindicator.h" #include <QPainter> CustomProgressIndicator::CustomProgressIndicator(QWidget* parent) : QWidget(parent), angle_(0), timerId_(-1), delay_(20), displayedWhenStopped_(false), color_(Qt::green) { setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed); setFocusPolicy(Qt::NoFocus); } bool CustomProgressIndicator::isAnimated () const { return (timerId_ != -1); } void CustomProgressIndicator::setDisplayedWhenStopped(bool state) { displayedWhenStopped_ = state; update(); } bool CustomProgressIndicator::isDisplayedWhenStopped() const { return displayedWhenStopped_; } void CustomProgressIndicator::startAnimation() { angle_ = 0; if (timerId_ == -1) { timerId_ = startTimer(delay_); } } void CustomProgressIndicator::stopAnimation() { if (timerId_ != -1) { killTimer(timerId_); } timerId_ = -1; update(); } void CustomProgressIndicator::setAnimationDelay(int delay) { if (timerId_ != -1){ killTimer(timerId_); } delay_ = delay; if (timerId_ != -1){ timerId_ = startTimer(delay_); } } void CustomProgressIndicator::setColor(const QColor & color) { color_ = color; update(); } QSize CustomProgressIndicator::sizeHint() const { return QSize(25,25); } void CustomProgressIndicator::timerEvent(QTimerEvent * ) { angle_ = (angle_+30)%360; update(); } void CustomProgressIndicator::paintEvent(QPaintEvent * ) { QPainter p(this); p.setRenderHint(QPainter::Antialiasing); if (!displayedWhenStopped_ && !isAnimated()) { p.drawPixmap(rect(),currentPix_); return; } int width = qMin(this->width(), this->height()); int outerRadius = (width-1) >> 1; int innerRadius = ((width-1) >> 1)*0.38; int capsuleHeight = outerRadius - innerRadius; int capsuleWidth = (width > 32 ) ? capsuleHeight *.23 : capsuleHeight *.35; int capsuleRadius = capsuleWidth >> 1; if (progress_ > 0 && progress_ < 100) { p.setPen(color_); p.drawText(rect(), Qt::AlignCenter, QString("%1%").arg(progress_)); } else if (progress_ == 100) { stopAnimation(); } for (int i=0; i<12; ++i) { QColor color = color_; color.setAlphaF(1.0f - (i/12.0f)); p.setPen(Qt::NoPen); p.setBrush(color); p.save(); p.translate(rect().center()); p.rotate(angle_ - i*30.0f); p.drawRoundedRect(((-capsuleWidth) >> 1), -(innerRadius+capsuleHeight), capsuleWidth, capsuleHeight, capsuleRadius, capsuleRadius); p.restore(); } }
|