Qt实现进度条(QProgressBar)

╰+攻爆jí腚メ 2023-02-13 04:59 108阅读 0赞

1、进度条的作用

  1. 用于显示时间,并告诉用户当前任务的执行进展。

2、进度条的使用方式

  1. 两种:模态方式和非模态方式
  2. 模态方式:使用简单,但必须使用QApplication::processEvents()使事件循环保持正常进行状态,以保证应用不会被阻塞。
  3. 非模态方式:需通过QTime实现定时设置进度条的值。

3、进度条的显示方式

  1. 两种:1QProgressBar,可以横向或纵向显示;2QProgressDialog,以对话框的形式表示。

4、标准进度条的基本元素

  1. 进度显示条、取消按钮、标签。

5、样例

1、头文件progressdlg.h

  1. #ifndef PROGRESSDLG_H
  2. #define PROGRESSDLG_H
  3. #include <QDialog>
  4. class QLabel;
  5. class QLineEdit;
  6. class QComboBox;
  7. class QProgressBar;
  8. class QPushButton;
  9. class QGridLayout;
  10. class ProgressDlg : public QDialog
  11. {
  12. Q_OBJECT
  13. public:
  14. ProgressDlg(QWidget *parent = 0);
  15. ~ProgressDlg();
  16. private slots:
  17. void startProgress();
  18. private:
  19. QLabel *FileNum;
  20. QLineEdit *FileNumLineEdit;
  21. QLabel *ProgressType;
  22. QComboBox *comboBox;
  23. QProgressBar *progressBar;
  24. QPushButton *starBtn;
  25. QGridLayout *mainLayout;
  26. };
  27. #endif // PROGRESSDLG_H

2、源文件 progressdlg.cpp

  1. #include "progressdlg.h"
  2. #include <QFont>
  3. #include <QLabel>
  4. #include <QLineEdit>
  5. #include <QComboBox>
  6. #include <QProgressBar>
  7. #include <QPushButton>
  8. #include <QGridLayout>
  9. #include <QProgressDialog>
  10. ProgressDlg::ProgressDlg(QWidget *parent)
  11. : QDialog(parent)
  12. {
  13. QFont font("ZYSong18030", 12);
  14. setFont(font);
  15. setWindowTitle(tr("Progress"));
  16. FileNum = new QLabel;
  17. FileNum->setText(tr("File Number: "));
  18. FileNumLineEdit = new QLineEdit;
  19. FileNumLineEdit->setText(tr("100000"));
  20. ProgressType = new QLabel;
  21. ProgressType->setText(tr("Dispaly Type: "));
  22. comboBox = new QComboBox;
  23. comboBox->addItem(tr("progressBar"));
  24. comboBox->addItem(tr("progressDialog"));
  25. progressBar = new QProgressBar;
  26. starBtn = new QPushButton;
  27. starBtn->setText(tr("Start"));
  28. mainLayout = new QGridLayout(this);
  29. mainLayout->addWidget(FileNum, 0, 0);
  30. mainLayout->addWidget(FileNumLineEdit, 0, 1);
  31. mainLayout->addWidget(ProgressType, 1, 0);
  32. mainLayout->addWidget(comboBox, 1, 1);
  33. mainLayout->addWidget(progressBar, 2, 0, 1, 2);
  34. mainLayout->addWidget(starBtn, 3, 1);
  35. mainLayout->setMargin(15);
  36. mainLayout->setSpacing(10);
  37. connect(starBtn, SIGNAL(clicked()), this, SLOT(startProgress()));
  38. }
  39. ProgressDlg::~ProgressDlg()
  40. {
  41. if(FileNum) delete FileNum;
  42. if(FileNumLineEdit) delete FileNumLineEdit;
  43. if(ProgressType) delete ProgressType;
  44. if(comboBox) delete comboBox;
  45. if(progressBar) delete progressBar;
  46. if(starBtn) delete starBtn;
  47. if(mainLayout) delete mainLayout;
  48. }
  49. void ProgressDlg::startProgress()
  50. {
  51. bool ok = false;
  52. int num = FileNumLineEdit->text().toInt(&ok);
  53. if (!ok)
  54. return ;
  55. if (comboBox->currentIndex() == 0)
  56. {
  57. progressBar->setRange(0, num);
  58. for (int i = 1; i < num + 1; ++i)
  59. {
  60. progressBar->setValue(i);
  61. }
  62. }
  63. else if (comboBox->currentIndex() == 1)
  64. {
  65. QProgressDialog *progressDialog = new QProgressDialog(this);
  66. QFont font("ZYSong18030", 12);
  67. progressDialog->setFont(font);
  68. progressDialog->setWindowModality(Qt::WindowModal);
  69. progressDialog->setMinimumDuration(5);
  70. progressDialog->setWindowTitle(tr("Please Wait"));
  71. progressDialog->setLabelText(tr("Copying..."));
  72. progressDialog->setCancelButtonText(tr("Cancel"));
  73. progressDialog->setRange(0, num);
  74. for (int i = 0; i < num + 1; ++i)
  75. {
  76. progressDialog->setValue(i);
  77. if (progressDialog->wasCanceled())
  78. {
  79. delete progressDialog;
  80. return ;
  81. }
  82. }
  83. delete progressDialog;
  84. }
  85. }

3、main.cpp

  1. #include "progressdlg.h"
  2. #include <QApplication>
  3. int main(int argc, char *argv[])
  4. {
  5. QApplication a(argc, argv);
  6. ProgressDlg w;
  7. w.show();
  8. return a.exec();
  9. }

6、显示结果

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

发表评论

表情:
评论列表 (有 0 条评论,108人围观)

还没有评论,来说两句吧...

相关阅读