├── test.gif ├── README.md ├── resource ├── radius_back.png ├── radius_front.png ├── progress_back.png ├── progress_front.png └── sequence_animate.png ├── .gitignore ├── main.cpp ├── customprogressbar.qrc ├── radiusprogressbar.h ├── ringsmapprogressbar.h ├── ringsprogressbar.h ├── mainwindow.h ├── animationprogressbar.h ├── progressdialog.h ├── mainwindow.cpp ├── radiusprogressbar.cpp ├── progressdialog.cpp ├── ringsprogressbar.cpp ├── ringsmapprogressbar.cpp ├── QtCustomProgressbar.pro ├── animationprogressbar.cpp └── mainwindow.ui /test.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Longxr/QtCustomProgressbar/HEAD/test.gif -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # CustomProgressbar 2 | 3 | 4 kinds of custom progress bar 4 | 5 | ![gif](test.gif) 6 | -------------------------------------------------------------------------------- /resource/radius_back.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Longxr/QtCustomProgressbar/HEAD/resource/radius_back.png -------------------------------------------------------------------------------- /resource/radius_front.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Longxr/QtCustomProgressbar/HEAD/resource/radius_front.png -------------------------------------------------------------------------------- /resource/progress_back.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Longxr/QtCustomProgressbar/HEAD/resource/progress_back.png -------------------------------------------------------------------------------- /resource/progress_front.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Longxr/QtCustomProgressbar/HEAD/resource/progress_front.png -------------------------------------------------------------------------------- /resource/sequence_animate.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Longxr/QtCustomProgressbar/HEAD/resource/sequence_animate.png -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # ignore user files 2 | *.user 3 | *.autosave 4 | 5 | # 忽略生成的中间文件 6 | 7 | **build** 8 | 9 | # 忽略一些草稿文件 10 | 11 | **script** 12 | -------------------------------------------------------------------------------- /main.cpp: -------------------------------------------------------------------------------- 1 | #include "mainwindow.h" 2 | #include 3 | 4 | int main(int argc, char *argv[]) 5 | { 6 | QApplication a(argc, argv); 7 | MainWindow w; 8 | w.show(); 9 | 10 | return a.exec(); 11 | } 12 | -------------------------------------------------------------------------------- /customprogressbar.qrc: -------------------------------------------------------------------------------- 1 | 2 | 3 | resource/progress_back.png 4 | resource/progress_front.png 5 | resource/sequence_animate.png 6 | resource/radius_back.png 7 | resource/radius_front.png 8 | 9 | 10 | -------------------------------------------------------------------------------- /radiusprogressbar.h: -------------------------------------------------------------------------------- 1 | #ifndef RADIUSPROGRESSBAR_H 2 | #define RADIUSPROGRESSBAR_H 3 | 4 | #include 5 | 6 | class RadiusProgressBar : public QProgressBar 7 | { 8 | Q_OBJECT 9 | public: 10 | explicit RadiusProgressBar(QWidget *parent = 0); 11 | 12 | signals: 13 | 14 | public slots: 15 | 16 | protected: 17 | void paintEvent(QPaintEvent *) Q_DECL_OVERRIDE; 18 | }; 19 | 20 | #endif // RADIUSPROGRESSBAR_H 21 | -------------------------------------------------------------------------------- /ringsmapprogressbar.h: -------------------------------------------------------------------------------- 1 | #ifndef RINGSMAPPROGRESSBAR_H 2 | #define RINGSMAPPROGRESSBAR_H 3 | 4 | #include 5 | 6 | class RingsMapProgressbar : public QWidget 7 | { 8 | Q_OBJECT 9 | 10 | public: 11 | explicit RingsMapProgressbar(QWidget *parent = 0); 12 | void setPersent(int persent); 13 | 14 | protected: 15 | void paintEvent(QPaintEvent *); 16 | 17 | private: 18 | int m_rotateAngle;//旋转角度 19 | int m_persent; //百分比 20 | }; 21 | 22 | #endif // RINGSMAPPROGRESSBAR_H 23 | -------------------------------------------------------------------------------- /ringsprogressbar.h: -------------------------------------------------------------------------------- 1 | #ifndef RINGSPROGRESSBAR_H 2 | #define RINGSPROGRESSBAR_H 3 | 4 | #include 5 | 6 | class RingsProgressbar : public QWidget 7 | { 8 | Q_OBJECT 9 | public: 10 | explicit RingsProgressbar(QWidget *parent = 0); 11 | void setRotateDelta(int delta); 12 | void setPersent(int persent); 13 | 14 | signals: 15 | 16 | public slots: 17 | 18 | protected: 19 | void paintEvent(QPaintEvent *); 20 | 21 | private: 22 | int m_rotateAngle;//旋转角度 23 | int m_persent; //百分比 24 | }; 25 | 26 | #endif // RINGSPROGRESSBAR_H 27 | -------------------------------------------------------------------------------- /mainwindow.h: -------------------------------------------------------------------------------- 1 | #ifndef MAINWINDOW_H 2 | #define MAINWINDOW_H 3 | 4 | #include 5 | #include 6 | 7 | #include "ringsmapprogressbar.h" 8 | 9 | namespace Ui { 10 | class MainWindow; 11 | } 12 | 13 | class MainWindow : public QMainWindow 14 | { 15 | Q_OBJECT 16 | 17 | public: 18 | explicit MainWindow(QWidget *parent = 0); 19 | ~MainWindow(); 20 | 21 | public slots: 22 | void updateProgressbar(); 23 | 24 | private: 25 | Ui::MainWindow *ui; 26 | QTimer *m_timer; 27 | int m_persent; 28 | 29 | RingsMapProgressbar *m_progressbar2; 30 | }; 31 | 32 | #endif // MAINWINDOW_H 33 | -------------------------------------------------------------------------------- /animationprogressbar.h: -------------------------------------------------------------------------------- 1 | #ifndef ANIMATIONPROGRESSBAR_H 2 | #define ANIMATIONPROGRESSBAR_H 3 | 4 | #include 5 | #include 6 | 7 | class AnimationProgressbar : public QWidget 8 | { 9 | Q_OBJECT 10 | 11 | public: 12 | AnimationProgressbar(QWidget *parent = 0); 13 | 14 | void startAnimation(); 15 | 16 | protected: 17 | void paintEvent(QPaintEvent*); 18 | 19 | private: 20 | QList m_animalist; 21 | QPropertyAnimation *m_animation; 22 | int m_animaindex; 23 | int m_animaTotal; 24 | int m_persent; 25 | 26 | private slots: 27 | void slot_valuechange(QVariant var); 28 | }; 29 | 30 | #endif // ANIMATIONPROGRESSBAR_H 31 | -------------------------------------------------------------------------------- /progressdialog.h: -------------------------------------------------------------------------------- 1 | #ifndef PROGRESSDIALOG_H 2 | #define PROGRESSDIALOG_H 3 | 4 | #include 5 | #include 6 | 7 | namespace Ui { 8 | class ProgressDialog; 9 | } 10 | 11 | class ProgressDialog : public QDialog 12 | { 13 | Q_OBJECT 14 | 15 | public: 16 | explicit ProgressDialog(QWidget *parent = 0); 17 | ~ProgressDialog(); 18 | 19 | QLabel *m_status; 20 | void setTitle(QString title); 21 | void setValue(int percent); 22 | void setProgressText(QString text); 23 | 24 | 25 | signals: 26 | void cancled(); 27 | 28 | private: 29 | Ui::ProgressDialog *ui; 30 | 31 | protected: 32 | void paintEvent(QPaintEvent *event) Q_DECL_OVERRIDE; 33 | private slots: 34 | void on_cancleButton_clicked(); 35 | }; 36 | 37 | #endif // PROGRESSDIALOG_H 38 | -------------------------------------------------------------------------------- /mainwindow.cpp: -------------------------------------------------------------------------------- 1 | #include "mainwindow.h" 2 | #include "ui_mainwindow.h" 3 | 4 | MainWindow::MainWindow(QWidget *parent) : 5 | QMainWindow(parent), 6 | ui(new Ui::MainWindow) 7 | { 8 | ui->setupUi(this); 9 | 10 | m_timer = new QTimer(this); 11 | m_persent = 0; 12 | connect(m_timer, SIGNAL(timeout()), this, SLOT(updateProgressbar())); 13 | 14 | m_timer->start(100); 15 | ui->bar4->startAnimation(); 16 | 17 | } 18 | 19 | MainWindow::~MainWindow() 20 | { 21 | delete ui; 22 | } 23 | 24 | void MainWindow::updateProgressbar() 25 | { 26 | if(m_persent >= 100){ 27 | m_persent = 0; 28 | } 29 | else{ 30 | m_persent += 5; 31 | } 32 | 33 | ui->bar1->setValue(m_persent); 34 | ui->bar2->setPersent(m_persent); 35 | ui->bar3->setPersent(m_persent); 36 | } 37 | -------------------------------------------------------------------------------- /radiusprogressbar.cpp: -------------------------------------------------------------------------------- 1 | #include "radiusprogressbar.h" 2 | #include 3 | 4 | RadiusProgressBar::RadiusProgressBar(QWidget *parent) : QProgressBar(parent) 5 | { 6 | setMinimum(0); 7 | setMaximum(100); 8 | setValue(0); 9 | } 10 | 11 | void RadiusProgressBar::paintEvent(QPaintEvent *) 12 | { 13 | QPainter p(this); 14 | QRect rect = QRect(0, 0, width(), height()/2); 15 | QRect textRect = QRect(0, height()/2, width(), height()/2); 16 | 17 | const double k = (double)(value() - minimum()) / (maximum()-minimum()); 18 | int x = (int)(rect.width() * k); 19 | QRect fillRect = rect.adjusted(0, 0, x-rect.width(), 0); 20 | 21 | QString valueStr = QString("%1%").arg(QString::number(value())); 22 | QPixmap buttomMap = QPixmap(":/resource/radius_back.png"); 23 | QPixmap fillMap = QPixmap(":/resource/radius_front.png"); 24 | 25 | //画进度条 26 | p.drawPixmap(rect, buttomMap); 27 | p.drawPixmap(fillRect, fillMap, fillRect); 28 | 29 | //画文字 30 | QFont f = QFont("Microsoft YaHei", 15, QFont::Bold); 31 | p.setFont(f); 32 | p.setPen(QColor("#555555")); 33 | p.drawText(textRect, Qt::AlignCenter, valueStr); 34 | } 35 | 36 | -------------------------------------------------------------------------------- /progressdialog.cpp: -------------------------------------------------------------------------------- 1 | #include "progressdialog.h" 2 | #include "ui_progressdialog.h" 3 | #include 4 | 5 | ProgressDialog::ProgressDialog(QWidget *parent) : 6 | QDialog(parent), 7 | ui(new Ui::ProgressDialog) 8 | { 9 | ui->setupUi(this); 10 | 11 | setAttribute(Qt::WA_TranslucentBackground, true); 12 | setWindowFlags(Qt::Window | Qt::FramelessWindowHint 13 | | Qt::WindowSystemMenuHint | Qt::WindowMinimizeButtonHint 14 | | Qt::WindowMaximizeButtonHint); 15 | 16 | ui->progressBar->setValue(0); 17 | } 18 | 19 | ProgressDialog::~ProgressDialog() 20 | { 21 | delete ui; 22 | } 23 | 24 | void ProgressDialog::paintEvent(QPaintEvent *) 25 | { 26 | QStyleOption opt; 27 | opt.init(this); 28 | QPainter p(this); 29 | style()->drawPrimitive(QStyle::PE_Widget, &opt, &p, this); 30 | } 31 | 32 | void ProgressDialog::setValue(int percent) 33 | { 34 | ui->progressBar->setValue(percent); 35 | } 36 | 37 | void ProgressDialog::setTitle(QString title) 38 | { 39 | ui->titleName->setText(title); 40 | } 41 | 42 | void ProgressDialog::setProgressText(QString text) 43 | { 44 | ui->progressLabel->setText(text); 45 | } 46 | 47 | void ProgressDialog::on_cancleButton_clicked() 48 | { 49 | ui->progressBar->setValue(0); 50 | emit cancled(); 51 | } 52 | -------------------------------------------------------------------------------- /ringsprogressbar.cpp: -------------------------------------------------------------------------------- 1 | #include "ringsprogressbar.h" 2 | #include 3 | 4 | RingsProgressbar::RingsProgressbar(QWidget *parent) : QWidget(parent), 5 | m_rotateAngle(0), 6 | m_persent(0) 7 | { 8 | 9 | } 10 | 11 | void RingsProgressbar::setPersent(int persent) 12 | { 13 | if(persent != m_persent) 14 | { 15 | m_persent = persent; 16 | update(); 17 | } 18 | } 19 | 20 | void RingsProgressbar::paintEvent(QPaintEvent *) 21 | { 22 | QPainter p(this); 23 | p.setRenderHint(QPainter::Antialiasing); 24 | 25 | m_rotateAngle = 360*m_persent/100; 26 | 27 | int side = qMin(width(), height()); 28 | QRectF outRect(0, 0, side, side); 29 | QRectF inRect(20, 20, side-40, side-40); 30 | QString valueStr = QString("%1%").arg(QString::number(m_persent)); 31 | 32 | //画外圆 33 | p.setPen(Qt::NoPen); 34 | p.setBrush(QBrush(QColor(97, 117, 118))); 35 | p.drawEllipse(outRect); 36 | p.setBrush(QBrush(QColor(255, 107, 107))); 37 | p.drawPie(outRect, (90-m_rotateAngle)*16, m_rotateAngle*16); 38 | //画遮罩 39 | p.setBrush(palette().window().color()); 40 | p.drawEllipse(inRect); 41 | //画文字 42 | QFont f = QFont("Microsoft YaHei", 15, QFont::Bold); 43 | p.setFont(f); 44 | p.setFont(f); 45 | p.setPen(QColor("#555555")); 46 | p.drawText(inRect, Qt::AlignCenter, valueStr); 47 | } 48 | -------------------------------------------------------------------------------- /ringsmapprogressbar.cpp: -------------------------------------------------------------------------------- 1 | #include "ringsmapprogressbar.h" 2 | #include 3 | 4 | RingsMapProgressbar::RingsMapProgressbar(QWidget *parent) : QWidget(parent), 5 | m_rotateAngle(0), 6 | m_persent(0) 7 | { 8 | 9 | } 10 | 11 | void RingsMapProgressbar::setPersent(int persent) 12 | { 13 | if(persent != m_persent) 14 | { 15 | m_persent = persent; 16 | update(); 17 | } 18 | } 19 | 20 | 21 | void RingsMapProgressbar::paintEvent(QPaintEvent *) 22 | { 23 | QPainter p(this); 24 | p.setRenderHint(QPainter::Antialiasing); 25 | 26 | m_rotateAngle = 360*m_persent/100; 27 | 28 | int side = qMin(width(), height()); 29 | QRectF outRect(0, 0, side, side); 30 | QRectF inRect(20, 20, side-40, side-40); 31 | QString valueStr = QString("%1%").arg(QString::number(m_persent)); 32 | 33 | //画底圆 34 | p.setPen(Qt::NoPen); 35 | QPixmap backMap = QPixmap(":/resource/progress_back.png"); 36 | p.drawPixmap(outRect, backMap, outRect); 37 | //画内弧 38 | QPixmap frontMap = QPixmap(":/resource/progress_front.png"); 39 | p.setBrush(QBrush(frontMap)); 40 | p.drawPie(outRect, (90-m_rotateAngle)*16, m_rotateAngle*16); 41 | //画文字 42 | QFont f = QFont("Microsoft YaHei", 15, QFont::Bold); 43 | p.setFont(f); 44 | p.setPen(QColor("#DDDDDD")); 45 | p.drawText(inRect, Qt::AlignCenter, valueStr); 46 | } 47 | 48 | -------------------------------------------------------------------------------- /QtCustomProgressbar.pro: -------------------------------------------------------------------------------- 1 | #------------------------------------------------- 2 | # 3 | # Project created by QtCreator 2017-11-16T22:14:41 4 | # 5 | #------------------------------------------------- 6 | 7 | QT += core gui 8 | 9 | greaterThan(QT_MAJOR_VERSION, 4): QT += widgets 10 | 11 | TARGET = QtCustomProgressbar 12 | TEMPLATE = app 13 | 14 | # The following define makes your compiler emit warnings if you use 15 | # any feature of Qt which as been marked as deprecated (the exact warnings 16 | # depend on your compiler). Please consult the documentation of the 17 | # deprecated API in order to know how to port your code away from it. 18 | DEFINES += QT_DEPRECATED_WARNINGS 19 | 20 | # You can also make your code fail to compile if you use deprecated APIs. 21 | # In order to do so, uncomment the following line. 22 | # You can also select to disable deprecated APIs only up to a certain version of Qt. 23 | #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0 24 | 25 | 26 | SOURCES += main.cpp\ 27 | mainwindow.cpp \ 28 | ringsprogressbar.cpp \ 29 | radiusprogressbar.cpp \ 30 | ringsmapprogressbar.cpp \ 31 | animationprogressbar.cpp 32 | 33 | HEADERS += mainwindow.h \ 34 | ringsprogressbar.h \ 35 | radiusprogressbar.h \ 36 | ringsmapprogressbar.h \ 37 | animationprogressbar.h 38 | 39 | FORMS += mainwindow.ui 40 | 41 | RESOURCES += \ 42 | customprogressbar.qrc 43 | -------------------------------------------------------------------------------- /animationprogressbar.cpp: -------------------------------------------------------------------------------- 1 | #include "animationprogressbar.h" 2 | #include 3 | 4 | AnimationProgressbar::AnimationProgressbar(QWidget *parent) : QWidget(parent), 5 | m_animaindex(0), 6 | m_animaTotal(30), 7 | m_persent(0) 8 | { 9 | QPixmap aniMap(":/resource/sequence_animate.png"); 10 | 11 | for(int i=0; isetStartValue(0); 17 | m_animation->setEndValue(m_animaTotal - 1); 18 | m_animation->setDuration(2000); 19 | m_animation->setLoopCount(-1); 20 | connect(m_animation,SIGNAL(valueChanged(QVariant)),this,SLOT(slot_valuechange(QVariant))); 21 | } 22 | 23 | void AnimationProgressbar::startAnimation() 24 | { 25 | update(); 26 | 27 | m_animation->start(); 28 | } 29 | 30 | void AnimationProgressbar::paintEvent(QPaintEvent *) 31 | { 32 | QPainter p(this); 33 | p.setRenderHint(QPainter::SmoothPixmapTransform); 34 | 35 | int side = qMin(width(), height()); 36 | QRect outRect(0, 0, side, side); 37 | QRect inRect(20, 20, side-40, side-40); 38 | QString valueStr = QString("%1%").arg(QString::number(m_persent)); 39 | 40 | //画圆环 41 | p.drawPixmap(outRect, m_animalist.at(m_animaindex)); 42 | 43 | //画文字 44 | QFont f = QFont("Microsoft YaHei", 15, QFont::Bold); 45 | p.setFont(f); 46 | p.setPen(QColor("#555555")); 47 | p.drawText(inRect, Qt::AlignCenter, valueStr); 48 | } 49 | 50 | void AnimationProgressbar::slot_valuechange(QVariant var) 51 | { 52 | m_animaindex = var.toInt(); 53 | m_persent = m_animaindex*100 / m_animaTotal; 54 | 55 | update(); 56 | } 57 | -------------------------------------------------------------------------------- /mainwindow.ui: -------------------------------------------------------------------------------- 1 | 2 | 3 | MainWindow 4 | 5 | 6 | 7 | 0 8 | 0 9 | 924 10 | 565 11 | 12 | 13 | 14 | MainWindow 15 | 16 | 17 | 18 | 19 | 20 | 500 21 | 50 22 | 200 23 | 200 24 | 25 | 26 | 27 | 28 | 29 | 30 | 120 31 | 270 32 | 200 33 | 200 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 500 44 | 270 45 | 200 46 | 200 47 | 48 | 49 | 50 | 51 | 52 | 53 | 120 54 | 150 55 | 200 56 | 61 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | RingsProgressbar 66 | QWidget 67 |
ringsprogressbar.h
68 | 1 69 |
70 | 71 | RingsMapProgressbar 72 | QWidget 73 |
ringsmapprogressbar.h
74 | 1 75 |
76 | 77 | AnimationProgressbar 78 | QWidget 79 |
animationprogressbar.h
80 | 1 81 |
82 | 83 | RadiusProgressBar 84 | QWidget 85 |
radiusprogressbar.h
86 | 1 87 |
88 |
89 | 90 | 91 |
92 | --------------------------------------------------------------------------------