鱼C论坛

 找回密码
 立即注册
查看: 6896|回复: 0

[技术原创] 菊花旋转完全用代码实现圆形加载进度动画 - C/C++进阶之Qt图形UI库

[复制链接]
发表于 2017-3-27 14:26:29 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
简述

初尝QT不甚欢喜,鱼C上有许多的朋友在为QT做出贡献,我也愿意以主观角度来分享QT,让QT变得更好。
该源码系作者个人撰写,使用时请尊重作者,不要随意篡改关于作者的信息。
版权所有:CSND_Ayo,转载请注明出处:http://blog.csdn.net/csnd_ayo

原帖地址:http://blog.csdn.net/csnd_ayo/article/details/56486455

源码


  1. #ifndef CUSTOMPROGRESSINDICATOR_H
  2. #define CUSTOMPROGRESSINDICATOR_H

  3. #include <QWidget>
  4. #include <QColor>
  5. /*
  6. * 菊花转 进度类,基于代码无需图片资源
  7. * 作者:陈鲁勇
  8. * 邮箱:727057301@qq.com
  9. * 创建时间:2017年2月10日16:26:48
  10. * QT版本:5.0.2
  11. * CSDN:http://blog.csdn.net/csnd_ayo
  12. * **************************************
  13. * 说明:
  14. *   使用前请确保在QT.pro中加入 C++11 的支持
  15. *
  16. * 示例代码:

  17.     pIndicator = new CustomProgressIndicator(this);
  18.     pIndicator->setColor(Qt::red);
  19.     pIndicator->startAnimation();
  20. */

  21. class CustomProgressIndicator : public QWidget
  22. {
  23.     Q_OBJECT
  24.     Q_PROPERTY(int delay READ animationDelay WRITE setAnimationDelay)
  25.     Q_PROPERTY(bool displayedWhenStopped READ isDisplayedWhenStopped WRITE setDisplayedWhenStopped)
  26.     Q_PROPERTY(QColor color READ color WRITE setColor)
  27. public:
  28.     CustomProgressIndicator(QWidget* parent = 0);

  29.     int animationDelay() const { return delay_; }

  30.     /*  动画是否正在进行中  */
  31.     bool isAnimated () const;

  32.     /*  动画完毕后,是否隐藏菊花转  */
  33.     bool isDisplayedWhenStopped() const;

  34.     /*  当前菊花转的颜色  */
  35.     const QColor & color() const { return color_; }

  36.     /*  虚函数:当前大小  */
  37.     virtual QSize sizeHint() const;

  38.     void setBackground(const QString& _icon) {
  39.         currentPix_ = QPixmap(_icon);
  40.     }
  41. signals:
  42.     void Finished(void);
  43. public slots:

  44.     /*  开始动画  */
  45.     void startAnimation();

  46.     /*  停止动画  */
  47.     void stopAnimation();

  48.     /*  设置菊花转的转速  */
  49.     void setAnimationDelay(int delay);

  50.     /*  动画完毕后,是否隐藏菊花转  */
  51.     void setDisplayedWhenStopped(bool state);

  52.     /*  设置菊花转颜色  */
  53.     void setColor(const QColor & color);

  54.     /*
  55.      * 进度
  56.      * 参数 _progress:当前进度 0 < _progress < 100
  57.     */
  58.     void onProgress(short _progress) { progress_ = _progress; }
  59. protected:
  60.     /*  系统基类函数 */
  61.     virtual void timerEvent(QTimerEvent * event);
  62.     virtual void paintEvent(QPaintEvent * event);
  63. private:
  64.     /*  角度  */
  65.     unsigned int angle_;
  66.     /*  定时器ID  */
  67.     int timerId_;
  68.     /*  转速  */
  69.     int delay_;
  70.     /*  是否隐藏  */
  71.     bool displayedWhenStopped_;
  72.     /*  菊花转颜色  */
  73.     QColor color_;
  74.     /*  进度  */
  75.     short progress_;
  76.     /*  背景图  */
  77.     QPixmap currentPix_;
  78. };


  79. #endif // CUSTOMPROGRESSINDICATOR_H
复制代码



  1. #include "customprogressindicator.h"
  2. #include <QPainter>

  3. CustomProgressIndicator::CustomProgressIndicator(QWidget* parent)
  4.     : QWidget(parent),
  5.       angle_(0),
  6.       timerId_(-1),
  7.       delay_(20),
  8.       displayedWhenStopped_(false),
  9.       color_(Qt::green) {
  10.     setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
  11.     setFocusPolicy(Qt::NoFocus);
  12. }

  13. bool CustomProgressIndicator::isAnimated () const {
  14.     return (timerId_ != -1);
  15. }

  16. void CustomProgressIndicator::setDisplayedWhenStopped(bool state) {
  17.     displayedWhenStopped_ = state;

  18.     update();
  19. }

  20. bool CustomProgressIndicator::isDisplayedWhenStopped() const {
  21.     return displayedWhenStopped_;
  22. }

  23. void CustomProgressIndicator::startAnimation() {
  24.     angle_ = 0;

  25.     if (timerId_ == -1) {
  26.        timerId_ = startTimer(delay_);
  27.     }
  28. }

  29. void CustomProgressIndicator::stopAnimation() {
  30.     if (timerId_ != -1) {
  31.         killTimer(timerId_);
  32.     }

  33.     timerId_ = -1;

  34.     update();
  35. }

  36. void CustomProgressIndicator::setAnimationDelay(int delay) {
  37.     if (timerId_ != -1){
  38.         killTimer(timerId_);
  39.     }

  40.     delay_ = delay;

  41.     if (timerId_ != -1){
  42.         timerId_ = startTimer(delay_);
  43.     }
  44. }

  45. void CustomProgressIndicator::setColor(const QColor & color) {
  46.     color_ = color;

  47.     update();
  48. }

  49. QSize CustomProgressIndicator::sizeHint() const {
  50.     return QSize(25,25);
  51. }


  52. void CustomProgressIndicator::timerEvent(QTimerEvent * /*event*/) {
  53.     angle_ = (angle_+30)%360;

  54.     update();
  55. }

  56. void CustomProgressIndicator::paintEvent(QPaintEvent * /*event*/) {
  57.     QPainter p(this);
  58.     p.setRenderHint(QPainter::Antialiasing);
  59.     if (!displayedWhenStopped_ && !isAnimated()) {
  60.         p.drawPixmap(rect(),currentPix_);
  61.         return;
  62.     }

  63.     int width = qMin(this->width(), this->height());


  64.     int outerRadius = (width-1) >> 1;
  65.     int innerRadius = ((width-1) >> 1)*0.38;

  66.     int capsuleHeight = outerRadius - innerRadius;
  67.     int capsuleWidth  = (width > 32 ) ? capsuleHeight *.23 : capsuleHeight *.35;
  68.     int capsuleRadius = capsuleWidth >> 1;

  69.     /* 撰写进度 */
  70.     if (progress_ > 0 && progress_ < 100) {
  71.         p.setPen(color_);
  72.         p.drawText(rect(), Qt::AlignCenter, QString("%1%").arg(progress_));
  73.     }
  74.     else if (progress_ == 100) {
  75.         stopAnimation();
  76.     }

  77.     for (int i=0; i<12; ++i) {
  78.         QColor color = color_;
  79.         color.setAlphaF(1.0f - (i/12.0f));
  80.         p.setPen(Qt::NoPen);
  81.         p.setBrush(color);
  82.         p.save();
  83.         p.translate(rect().center());
  84.         p.rotate(angle_ - i*30.0f);
  85.         p.drawRoundedRect(((-capsuleWidth) >> 1), -(innerRadius+capsuleHeight), capsuleWidth, capsuleHeight, capsuleRadius, capsuleRadius);
  86.         p.restore();
  87.     }
  88. }
复制代码

小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-6-4 04:48

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表