操作系统:Fedora Linux 14 效果截图: 创建文件夹Progress,以下是代码(三个文件) /** Object: Progress ** Desc: 使用进度条 ** File: main.cpp ** Compile:qmake-qt4 -project;qmake-qt4;make; ** Author: LiXiujie www.xiujie.cn ** Date: 2011-05-16 ** Note: 编译说明: ** qmake-qt4 -prject 自动生成程序的项目文件(*.pro); ** qmake-qt4 用于生成程序的Makefile文件; ** make 编译 Makefile 文件得到可执行文件。 ** */ #include <QApplication> // 所有QT图形化应用程序必须包含此文件,它包含了QT图形化应用程序的各种资源、基本设置、控制流及事件处理等。 #include "Progress.h" // 自定义类头文件 int main(int argc, char *argv[]){ QApplication app(argc, argv); Progress *pProgress = new Progress(); pProgress->show(); return app.exec(); } /** Object: Progress ** Desc: 使用进度条 ** File: Progress.h ** Class: Progress 使用进度条 头文件 ** Compile:qmake-qt4 -project;qmake-qt4;make; ** Author: LiXiujie www.xiujie.cn ** Date: 2011-05-16 ** Note: 编译说明: ** qmake-qt4 -prject 自动生成程序的项目文件(*.pro); ** qmake-qt4 用于生成程序的Makefile文件; ** make 编译 Makefile 文件得到可执行文件。 ** */ #ifndef PROGRESS_H #define PROGRESS_H #include <QtGui> class Progress:public QDialog{ Q_OBJECT public: Progress(QWidget *parent=0, Qt::WindowFlags f=0); ~Progress(); public: QLabel *m_pLabelNum; QLineEdit *m_pLineEditNum; QLabel *m_pLabelType; QComboBox *m_pComboBoxType; QProgressBar *m_pProgressBar; QPushButton *m_pPBStart; private slots: void slotPBStart(); }; #endif // PROGRESS_H /** Object: Progress ** Desc: 使用进度条 ** File: Progress.cpp ** Class: Progress 使用进度条 源文件 ** Compile:qmake-qt4 -project;qmake-qt4;make; ** Author: LiXiujie www.xiujie.cn ** Date: 2011-05-16 ** Note: 编译说明: ** qmake-qt4 -prject 自动生成程序的项目文件(*.pro); ** qmake-qt4 用于生成程序的Makefile文件; ** make 编译 Makefile 文件得到可执行文件。 ** */ #include "Progress.h" #include <QtTest> // To link against the module, add this line to your qmake .pro file: CONFIG += qtestlib Progress::Progress(QWidget *parent, Qt::WindowFlags f):QDialog(parent, f){ setWindowTitle(tr("Progress")); // 设置窗口标题 QGridLayout *pGLayout = new QGridLayout(this); // 表格布局控件 m_pLabelNum = new QLabel(tr("File Number:")); m_pLineEditNum = new QLineEdit; m_pLineEditNum->setText(tr("10")); m_pLabelType = new QLabel(tr("Progress Type:")); m_pComboBoxType = new QComboBox; m_pComboBoxType->addItem(tr("ProgressBar")); m_pComboBoxType->addItem(tr("ProgressDialog")); m_pProgressBar = new QProgressBar; m_pProgressBar->setFormat("%p%, %v/%m"); // 设置进度条上文本样式 ,默认%p%, %p 百分比, %v 当前值, %m 总的步进值 m_pPBStart = new QPushButton(tr("Start")); pGLayout->addWidget( m_pLabelNum, 0, 0 ); pGLayout->addWidget( m_pLineEditNum, 0, 1 ); pGLayout->addWidget( m_pLabelType, 1, 0 ); pGLayout->addWidget( m_pComboBoxType, 1, 1 ); pGLayout->addWidget( m_pProgressBar, 2, 0, 1, 2 ); // 第三行,第一列,占一行,占二列 pGLayout->addWidget( m_pPBStart, 3, 1 ); pGLayout->setMargin(15); // 设置布局四边距 pGLayout->setSpacing(10); // 设置布局内部单元格边距 connect(m_pPBStart, SIGNAL(clicked()), this, SLOT(slotPBStart())); // 绑定按钮点击事件处理函数 } Progress::~Progress(){ } void Progress::slotPBStart(){ int nNum = m_pLineEditNum->text().toInt(); // 文本框中整数值 if(m_pComboBoxType->currentIndex() == 0) { // ProgressBar 进度条 m_pProgressBar->setRange(0, nNum); // 设置进度条大小值 for (int i=1; i<nNum+1; i++){ m_pProgressBar->setValue(i); /* 复制处理 */ QTest::qWait(1000); // 等待1秒 } } else if(m_pComboBoxType->currentIndex() == 1) { // modal ProgressDialog 模态进度对话框 QProgressDialog *pProgressDialog = new QProgressDialog(this); pProgressDialog->setWindowModality(Qt::WindowModal); // 设置为模态窗口 pProgressDialog->setMinimumDuration(5); // 设置最小持续时间 pProgressDialog->setWindowTitle(tr("Please Wait")); //设置进度对话框中 pProgressDialog->setLabelText(tr("Copying...")); // 设置显示标签内容 pProgressDialog->setCancelButtonText(tr("Cancel"));// 设置取消按钮内容 pProgressDialog->setRange(0, nNum); // 设置进度对话框中进度条大小值 for (int i=1; i<nNum+1; i++) { pProgressDialog->setValue(i); qApp->processEvents(); // qApp 全局程序对象指针, qApp->processEvents() 来正常响应事件循环,确保应用程序不会出现阻塞 /* 复制处理 */ QTest::qWait(1000); // 等待延时多少毫秒 if (pProgressDialog->wasCanceled()) // 检测进度对话框中”取消“按钮是否被触发。 return; } } }