├── screenshot.gif ├── main.cpp ├── Example.h ├── README.md ├── .gitignore ├── LICENSE ├── QProcessIndicator.pro ├── Example.cpp └── QProcessIndicator ├── QProcessIndicator.h └── QProcessIndicator.cpp /screenshot.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/raythorn/QProcessIndicator/HEAD/screenshot.gif -------------------------------------------------------------------------------- /main.cpp: -------------------------------------------------------------------------------- 1 | #include "Example.h" 2 | #include 3 | 4 | int main(int argc, char *argv[]) 5 | { 6 | QApplication a(argc, argv); 7 | Example w; 8 | w.show(); 9 | 10 | return a.exec(); 11 | } 12 | -------------------------------------------------------------------------------- /Example.h: -------------------------------------------------------------------------------- 1 | #ifndef EXAMPLE_H 2 | #define EXAMPLE_H 3 | 4 | #include 5 | 6 | class Example : public QWidget 7 | { 8 | Q_OBJECT 9 | 10 | public: 11 | explicit Example(QWidget *parent = 0); 12 | ~Example(); 13 | 14 | private: 15 | 16 | }; 17 | 18 | #endif // EXAMPLE_H 19 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # QProcessIndicator 2 | QProcessIndicator is a Loading/Busy indicator widget for Qt 3 | 4 | # ScreenShot 5 | ![](https://github.com/raythorn/QProcessIndicator/blob/master/screenshot.gif) 6 | 7 | #LICENSE 8 | QProcessIndicator source code is licensed under the [Apache License](http://www.apache.org/licenses/LICENSE-2.0.html), Version 2.0. 9 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # C++ objects and libs 2 | 3 | *.slo 4 | *.lo 5 | *.o 6 | *.a 7 | *.la 8 | *.lai 9 | *.so 10 | *.dll 11 | *.dylib 12 | 13 | # Qt-es 14 | 15 | /.qmake.cache 16 | /.qmake.stash 17 | *.pro.user 18 | *.pro.user.* 19 | *.qbs.user 20 | *.qbs.user.* 21 | *.moc 22 | moc_*.cpp 23 | qrc_*.cpp 24 | ui_*.h 25 | Makefile* 26 | *build-* 27 | 28 | # QtCreator 29 | 30 | *.autosave 31 | 32 | # QtCtreator Qml 33 | *.qmlproject.user 34 | *.qmlproject.user.* 35 | 36 | # QtCtreator CMake 37 | CMakeLists.txt.user 38 | 39 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright 2017 Derek Ray(derek9ray@gmail.com) 2 | 3 | Licensed under the Apache License, Version 2.0 (the "License"); 4 | you may not use this file except in compliance with the License. 5 | You may obtain a copy of the License at 6 | 7 | http://www.apache.org/licenses/LICENSE-2.0 8 | 9 | Unless required by applicable law or agreed to in writing, software 10 | distributed under the License is distributed on an "AS IS" BASIS, 11 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | See the License for the specific language governing permissions and 13 | limitations under the License. 14 | -------------------------------------------------------------------------------- /QProcessIndicator.pro: -------------------------------------------------------------------------------- 1 | #------------------------------------------------- 2 | # 3 | # Project created by QtCreator 2017-01-09T22:08:55 4 | # 5 | #------------------------------------------------- 6 | 7 | QT += core gui 8 | 9 | greaterThan(QT_MAJOR_VERSION, 4): QT += widgets 10 | 11 | TARGET = QProcessIndicator 12 | TEMPLATE = app 13 | 14 | 15 | SOURCES += main.cpp\ 16 | QProcessIndicator/QProcessIndicator.cpp \ 17 | Example.cpp 18 | 19 | HEADERS += QProcessIndicator/QProcessIndicator.h \ 20 | Example.h 21 | 22 | DISTFILES += \ 23 | build/debug/QProcessIndicator.ilk \ 24 | build/debug/QProcessIndicator.pdb \ 25 | build/debug/QProcessIndicator.exe 26 | 27 | FORMS += 28 | -------------------------------------------------------------------------------- /Example.cpp: -------------------------------------------------------------------------------- 1 | #include "Example.h" 2 | 3 | #include "QProcessIndicator/QProcessIndicator.h" 4 | 5 | Example::Example(QWidget *parent) : 6 | QWidget(parent) 7 | { 8 | setFixedSize(256, 64); 9 | 10 | QProcessIndicator *indicator = new QProcessIndicator(this); 11 | indicator->setGeometry(16, 16, 64, 32); 12 | indicator->setType(QProcessIndicator::line_rotate); 13 | indicator->start(); 14 | 15 | indicator = new QProcessIndicator(this); 16 | indicator->setGeometry(96, 16, 64, 32); 17 | indicator->setType(QProcessIndicator::ball_rotate); 18 | indicator->start(); 19 | 20 | indicator = new QProcessIndicator(this); 21 | indicator->setGeometry(176, 16, 64, 32); 22 | indicator->setType(QProcessIndicator::line_scale); 23 | indicator->setInterval(125); 24 | indicator->start(); 25 | } 26 | 27 | Example::~Example() 28 | { 29 | } 30 | -------------------------------------------------------------------------------- /QProcessIndicator/QProcessIndicator.h: -------------------------------------------------------------------------------- 1 | #ifndef QPROCESSINDICATOR_H 2 | #define QPROCESSINDICATOR_H 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | 10 | class QProcessIndicator : public QWidget 11 | { 12 | Q_OBJECT 13 | 14 | Q_PROPERTY(int m_type READ type WRITE setType) 15 | Q_PROPERTY(QColor m_color READ color WRITE setColor) 16 | Q_PROPERTY(int m_interval READ interval WRITE setInterval) 17 | 18 | public: 19 | QProcessIndicator(QWidget *parent = 0); 20 | ~QProcessIndicator(); 21 | 22 | enum { 23 | line_rotate, 24 | line_scale, 25 | ball_rotate, 26 | }; 27 | 28 | void paintEvent(QPaintEvent *e); 29 | 30 | void start(); 31 | void stop(); 32 | 33 | int type() {return m_type;} 34 | void setType(int type) {m_type = type;} 35 | 36 | QColor &color() {return m_color;} 37 | void setColor(QColor &color) {m_color = color;} 38 | 39 | int interval() {return m_interval;} 40 | void setInterval(int interval) {m_interval = interval;} 41 | 42 | private slots: 43 | void onTimeout(); 44 | 45 | private: 46 | void drawRotateLine(QPainter *painter); 47 | void drawScaleLine(QPainter *painter); 48 | void drawRotateBall(QPainter *painter); 49 | 50 | private: 51 | int m_type; 52 | int m_interval; 53 | QColor m_color; 54 | 55 | int m_angle; 56 | qreal m_scale; 57 | 58 | QTimer *m_timer; 59 | }; 60 | 61 | #endif // QPROCESSINDICATOR_H 62 | -------------------------------------------------------------------------------- /QProcessIndicator/QProcessIndicator.cpp: -------------------------------------------------------------------------------- 1 | #include "QProcessIndicator.h" 2 | 3 | #include 4 | #include 5 | #include 6 | 7 | #define SPIN_INTERVAL 60 8 | 9 | QProcessIndicator::QProcessIndicator(QWidget *parent) 10 | : QWidget(parent), 11 | m_type(line_rotate), 12 | m_interval(SPIN_INTERVAL), 13 | m_angle(0), 14 | m_scale(0.0f), 15 | m_color(Qt::black) 16 | { 17 | setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed); 18 | setFocusPolicy(Qt::NoFocus); 19 | 20 | m_timer = new QTimer(); 21 | connect(m_timer, SIGNAL(timeout()), this, SLOT(onTimeout())); 22 | } 23 | 24 | QProcessIndicator::~QProcessIndicator() 25 | { 26 | stop(); 27 | 28 | delete m_timer; 29 | } 30 | 31 | void QProcessIndicator::paintEvent(QPaintEvent *e) 32 | { 33 | Q_UNUSED(e) 34 | 35 | if(!m_timer->isActive()) 36 | { 37 | return; 38 | } 39 | 40 | QPainter painter(this); 41 | painter.setRenderHint(QPainter::Antialiasing); 42 | 43 | switch(m_type) 44 | { 45 | case line_rotate: 46 | drawRotateLine(&painter); 47 | break; 48 | case line_scale: 49 | drawScaleLine((&painter)); 50 | break; 51 | case ball_rotate: 52 | drawRotateBall(&painter); 53 | break; 54 | } 55 | } 56 | 57 | void QProcessIndicator::start() 58 | { 59 | m_timer->start(m_interval); 60 | } 61 | 62 | void QProcessIndicator::stop() 63 | { 64 | m_timer->stop(); 65 | } 66 | 67 | void QProcessIndicator::onTimeout() 68 | { 69 | switch(m_type) 70 | { 71 | case line_rotate: 72 | case ball_rotate: 73 | m_angle = (m_angle + 45) % 360; 74 | break; 75 | case line_scale: 76 | m_scale += 0.1f; 77 | m_scale = m_scale > .5f ? 0.0f : m_scale; 78 | break; 79 | } 80 | 81 | update(); 82 | } 83 | 84 | void QProcessIndicator::drawRotateLine(QPainter *painter) 85 | { 86 | int width = qMin(this->width(), this->height()); 87 | 88 | int outerRadius = (width - 4) * 0.5f; 89 | int innerRadius = outerRadius * 0.42f; 90 | 91 | int capsuleHeight = outerRadius - innerRadius; 92 | int capsuleWidth = (width > 32 ) ? capsuleHeight * .32f : capsuleHeight * .40f; 93 | int capsuleRadius = capsuleWidth / 2; 94 | 95 | for (int i = 0; i < 8; i++) 96 | { 97 | QColor color = m_color; 98 | 99 | color.setAlphaF(1.0f - (i / 8.0f)); 100 | painter->setPen(Qt::NoPen); 101 | painter->setBrush(color); 102 | 103 | painter->save(); 104 | 105 | painter->translate(rect().center()); 106 | painter->rotate(m_angle - i * 45.0f); 107 | 108 | painter->drawRoundedRect(-capsuleWidth * 0.5, -(innerRadius + capsuleHeight), capsuleWidth, capsuleHeight, capsuleRadius, capsuleRadius); 109 | 110 | painter->restore(); 111 | } 112 | } 113 | 114 | void QProcessIndicator::drawScaleLine(QPainter *painter) 115 | { 116 | int height = qMin(this->width(), this->height()); 117 | 118 | qreal lineWidth = height * 0.15f; 119 | qreal lineHeight = height * 0.9f; 120 | qreal lineRadius = lineWidth / 2.0f; 121 | qreal lineGap = lineWidth; 122 | qreal margin = (this->width() - lineWidth * 5 - lineGap * 4) / 2.0f; 123 | 124 | for(int i = 0; i < 5; i++) 125 | { 126 | painter->setPen(Qt::NoPen); 127 | painter->setBrush(m_color); 128 | 129 | int tmp = m_scale * 10 + i + 1; 130 | if(tmp > 5) 131 | { 132 | tmp = 5 - tmp % 5; 133 | } 134 | 135 | qDebug() << tmp; 136 | 137 | qreal scale = 0.5f + tmp * 0.1f; 138 | qreal h = lineHeight * scale; 139 | 140 | painter->save(); 141 | 142 | painter->translate(QPointF(margin + (lineWidth + lineGap) * i, this->height() / 2)); 143 | 144 | painter->drawRoundedRect(0, -h / 2.0f, lineWidth, h, lineRadius, lineRadius); 145 | 146 | painter->restore(); 147 | } 148 | } 149 | 150 | void QProcessIndicator::drawRotateBall(QPainter *painter) 151 | { 152 | int width = qMin(this->width(), this->height()); 153 | 154 | int outerRadius = (width - 4) * 0.5f; 155 | int innerRadius = outerRadius * 0.78f; 156 | 157 | int capsuleRadius = (outerRadius - innerRadius) / 2; 158 | 159 | for(int i = 0; i < 8; i++) 160 | { 161 | QColor color = m_color; 162 | 163 | color.setAlphaF(1.0f - (i / 8.0f)); 164 | 165 | painter->setPen(Qt::NoPen); 166 | painter->setBrush(color); 167 | 168 | qreal radius = capsuleRadius * (1.0f - (i / 16.0f)); 169 | 170 | painter->save(); 171 | 172 | painter->translate(rect().center()); 173 | painter->rotate(m_angle - i * 45.0f); 174 | 175 | QPointF centre = QPointF(-capsuleRadius, -(innerRadius + capsuleRadius)); 176 | painter->drawEllipse(centre, radius * 2, radius * 2); 177 | 178 | painter->restore(); 179 | } 180 | } 181 | --------------------------------------------------------------------------------