当前位置: 首页 > 网络知识

Qt 之 QProgressIndicator(等待提示框)

时间:2026-01-29 09:27:09

效果

  由于录制程序的原因,引起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 28 29 /*! Returns a Boolean value indicating whether the cponent 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 cponent. 42 \sa setColor 43 */ 44 const QColor & color() const 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 */ 64 void setAnimationDelay(int delay); 65 66 /*! Sets whether the cponent 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 cponents 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

QProgressIndicatorpp

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 * /*event*/) 81 86 87 void QProgressIndicator::paintEvent(QPaintEvent * /*event*/) 88 116 }

使用

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

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



上一篇:Qt QSettings读写ini时 General 读不出来值
下一篇:Siemens 西门子 S7协议及报文格式详解
Qt
  • 英特尔与 Vertiv 合作开发液冷 AI 处理器
  • 英特尔第五代 Xeon CPU 来了:详细信息和行业反应
  • 由于云计算放缓引发扩张担忧,甲骨文股价暴跌
  • Web开发状况报告详细介绍可组合架构的优点
  • 如何使用 PowerShell 的 Get-Date Cmdlet 创建时间戳
  • 美光在数据中心需求增长后给出了强有力的预测
  • 2027服务器市场价值将接近1960亿美元
  • 生成式人工智能的下一步是什么?
  • 分享在外部存储上安装Ubuntu的5种方法技巧
  • 全球数据中心发展的关键考虑因素
  • 英特尔与 Vertiv 合作开发液冷 AI 处理器

    英特尔第五代 Xeon CPU 来了:详细信息和行业反应

    由于云计算放缓引发扩张担忧,甲骨文股价暴跌

    Web开发状况报告详细介绍可组合架构的优点

    如何使用 PowerShell 的 Get-Date Cmdlet 创建时间戳

    美光在数据中心需求增长后给出了强有力的预测

    2027服务器市场价值将接近1960亿美元

    生成式人工智能的下一步是什么?

    分享在外部存储上安装Ubuntu的5种方法技巧

    全球数据中心发展的关键考虑因素