一杯清酒邀明月
天下本无事,庸人扰之而烦耳。
posts - 3121,comments - 209,views - 578万

效果

  由于录制程序的原因,引起gif效果不清晰,可忽略。

源码

QProgressIndicator.h

复制代码
 1 #ifndef QPROGRESSINDICATOR_H
 2 #define QPROGRESSINDICATOR_H
 3 
 4 #include <QWidget>
 5 #include <QColor>
 6 
 7 /*! 
 8     \class QProgressIndicator
 9     \brief The QProgressIndicator class lets an application display a progress indicator to show that a lengthy task is under way. 
10 
11     Progress indicators are indeterminate and do nothing more than spin to show that the application is busy.
12     \sa QProgressBar
13 */
14 class QProgressIndicator : public QWidget
15 {
16     Q_OBJECT
17     Q_PROPERTY(int delay READ animationDelay WRITE setAnimationDelay)
18     Q_PROPERTY(bool displayedWhenStopped READ isDisplayedWhenStopped WRITE setDisplayedWhenStopped)
19     Q_PROPERTY(QColor color READ color WRITE setColor)
20 public:
21     QProgressIndicator(QWidget* parent = 0);
22 
23     /*! Returns the delay between animation steps.
24         \return The number of milliseconds between animation steps. By default, the animation delay is set to 40 milliseconds.
25         \sa setAnimationDelay
26      */
27     int animationDelay() const { return m_delay; }
28 
29     /*! Returns a Boolean value indicating whether the component is currently animated.
30         \return Animation state.
31         \sa startAnimation stopAnimation
32      */
33     bool isAnimated () const;
34 
35     /*! Returns a Boolean value indicating whether the receiver shows itself even when it is not animating.
36         \return Return true if the progress indicator shows itself even when it is not animating. By default, it returns false.
37         \sa setDisplayedWhenStopped
38      */
39     bool isDisplayedWhenStopped() const;
40 
41     /*! Returns the color of the component.
42         \sa setColor
43       */
44     const QColor & color() const { return m_color; }
45 
46     virtual QSize sizeHint() const;
47     int heightForWidth(int w) const;
48 public slots:
49     /*! Starts the spin animation.
50         \sa stopAnimation isAnimated
51      */
52     void startAnimation();
53 
54     /*! Stops the spin animation.
55         \sa startAnimation isAnimated
56      */
57     void stopAnimation();
58 
59     /*! Sets the delay between animation steps.
60         Setting the \a delay to a value larger than 40 slows the animation, while setting the \a delay to a smaller value speeds it up.
61         \param delay The delay, in milliseconds. 
62         \sa animationDelay 
63      */
64     void setAnimationDelay(int delay);
65 
66     /*! Sets whether the component hides itself when it is not animating. 
67        \param state The animation state. Set false to hide the progress indicator when it is not animating; otherwise true.
68        \sa isDisplayedWhenStopped
69      */
70     void setDisplayedWhenStopped(bool state);
71 
72     /*! Sets the color of the components to the given color.
73         \sa color
74      */
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_H
复制代码

QProgressIndicator.cpp

复制代码
  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 {
 13     setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
 14     setFocusPolicy(Qt::NoFocus);
 15 }
 16 
 17 bool QProgressIndicator::isAnimated () const
 18 {
 19     return (m_timerId != -1);
 20 }
 21 
 22 void QProgressIndicator::setDisplayedWhenStopped(bool state)
 23 {
 24     m_displayedWhenStopped = state;
 25 
 26     update();
 27 }
 28 
 29 bool QProgressIndicator::isDisplayedWhenStopped() const
 30 {
 31     return m_displayedWhenStopped;
 32 }
 33 
 34 void QProgressIndicator::startAnimation()
 35 {
 36     m_angle = 0;
 37 
 38     if (m_timerId == -1)
 39         m_timerId = startTimer(m_delay);
 40 }
 41 
 42 void QProgressIndicator::stopAnimation()
 43 {
 44     if (m_timerId != -1)
 45         killTimer(m_timerId);
 46 
 47     m_timerId = -1;
 48 
 49     update();
 50 }
 51 
 52 void QProgressIndicator::setAnimationDelay(int delay)
 53 {
 54     if (m_timerId != -1)
 55         killTimer(m_timerId);
 56 
 57     m_delay = delay;
 58 
 59     if (m_timerId != -1)
 60         m_timerId = startTimer(m_delay);
 61 }
 62 
 63 void QProgressIndicator::setColor(const QColor & color)
 64 {
 65     m_color = color;
 66 
 67     update();
 68 }
 69 
 70 QSize QProgressIndicator::sizeHint() const
 71 {
 72     return QSize(20,20);
 73 }
 74 
 75 int QProgressIndicator::heightForWidth(int w) const
 76 {
 77     return w;
 78 }
 79 
 80 void QProgressIndicator::timerEvent(QTimerEvent * /*event*/)
 81 {
 82     m_angle = (m_angle+30)%360;
 83 
 84     update();
 85 }
 86 
 87 void QProgressIndicator::paintEvent(QPaintEvent * /*event*/)
 88 {
 89     if (!m_displayedWhenStopped && !isAnimated())
 90         return;
 91 
 92     int width = qMin(this->width(), this->height());
 93     
 94     QPainter p(this);
 95     p.setRenderHint(QPainter::Antialiasing);
 96     
 97     int outerRadius = (width-1)*0.5;
 98     int innerRadius = (width-1)*0.5*0.38;
 99 
100     int capsuleHeight = outerRadius - innerRadius;
101     int capsuleWidth  = (width > 32 ) ? capsuleHeight *.23 : capsuleHeight *.35;
102     int capsuleRadius = capsuleWidth/2;
103 
104     for (int i=0; i<12; i++)
105     {
106         QColor color = m_color;
107         color.setAlphaF(1.0f - (i/12.0f));
108         p.setPen(Qt::NoPen);
109         p.setBrush(color);       
110         p.save();
111         p.translate(rect().center());
112         p.rotate(m_angle - i*30.0f);
113         p.drawRoundedRect(-capsuleWidth*0.5, -(innerRadius+capsuleHeight), capsuleWidth, capsuleHeight, capsuleRadius, capsuleRadius);
114         p.restore();
115     }
116 }
复制代码

使用

1 QProgressIndicator *pIndicator = new QProgressIndicator(this);
2 pIndicator->setColor(Qt::white);
3 pIndicator->startAnimation();

源码没什么难度,有兴趣的可以根据需要自行修改。

posted on   一杯清酒邀明月  阅读(107)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 全网最简单!3分钟用满血DeepSeek R1开发一款AI智能客服,零代码轻松接入微信、公众号、小程
· .NET 10 首个预览版发布,跨平台开发与性能全面提升
· 《HelloGitHub》第 107 期
· 全程使用 AI 从 0 到 1 写了个小工具
· 从文本到图像:SSE 如何助力 AI 内容实时呈现?(Typescript篇)
历史上的今天:
2020-05-22 Qt 错误:QMainWindow: No such file or directory 解决方法
2020-05-22 Qt 错误:QtGui/QApplication在Qt5没有这个文件
< 2025年3月 >
23 24 25 26 27 28 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 1 2 3 4 5

点击右上角即可分享
微信分享提示