效果
由于录制程序的原因,引起gif效果不清晰,可忽略。
源码
QProgressIndicator.h
1 #ifndef QPROGRESSINDICATOR_H 2 #define QPROGRESSINDICATOR_H 3 4 #include <QWidget> 5 #include <QColor> 6 7 14 class QProgressIndicator : public QWidget 15 28 29 33 bool isAnimated () const; 34 35 39 bool isDisplayedWhenStopped() const; 40 41 44 const QColor & color() const 45 46 virtual QSize sizeHint() const; 47 int heightForWidth(int w) const; 48 public slots: 49 52 void startAnimation(); 53 54 57 void stopAnimation(); 58 59 64 void setAnimationDelay(int delay); 65 66 70 void setDisplayedWhenStopped(bool state); 71 72 75 void setColor(const QColor & color); 76 protected: 77 virtual void timerEvent(QTimerEvent * event); 78 virtual void paintEvent(QPaintEvent * event); 79 private: 80 int m_angle; 81 int m_timerId; 82 int m_delay; 83 bool m_displayedWhenStopped; 84 QColor m_color; 85 }; 86 87 #endif // QPROGRESSINDICATOR_HQProgressIndicatorpp
1 #include "QProgressIndicator.h" 2 3 #include <QPainter> 4 5 QProgressIndicator::QProgressIndicator(QWidget* parent) 6 : QWidget(parent), 7 m_angle(0), 8 m_timerId(1), 9 m_delay(40), 10 m_displayedWhenStopped(false), 11 m_color(Qt::black) 12 16 17 bool QProgressIndicator::isAnimated () const 18 21 22 void QProgressIndicator::setDisplayedWhenStopped(bool state) 23 28 29 bool QProgressIndicator::isDisplayedWhenStopped() const 30 33 34 void QProgressIndicator::startAnimation() 35 41 42 void QProgressIndicator::stopAnimation() 43 51 52 void QProgressIndicator::setAnimationDelay(int delay) 53 62 void QProgressIndicator::setColor(const QColor & color) 64 69 70 QSize QProgressIndicator::sizeHint() const 71 74 75 int QProgressIndicator::heightForWidth(int w) const 76 79 80 void QProgressIndicator::timerEvent(QTimerEvent * ) 81 86 87 void QProgressIndicator::paintEvent(QPaintEvent * ) 88 116 }使用
1 QProgressIndicator *pIndicator = new QProgressIndicator(this); 2 pIndicator>setColor(Qt::white); 3 pIndicator>startAnimation();源码没什么难度,有兴趣的可以根据需要自行修改。