QT中progress的使用
2012-04-10 12:26 Rollen Holt 阅读(4231) 评论(0) 编辑 收藏 举报progress。h
#ifndef PROGRESS_H #define PROGRESS_H #include <QtGui> #include <QtTest> #include "ui_progress.h" class progress : public QDialog { Q_OBJECT public: progress(QWidget *parent = 0, Qt::WFlags flags = 0); ~progress(); QLabel *numLabel; QLineEdit *numLineEdit; QLabel *typeLabel; QComboBox *typeComboBox; QProgressBar *progressBar; QPushButton *startPushButton; private: Ui::progressClass ui; private slots: void slotStart(); }; #endif // PROGRESS_H
progress.cpp
#include "progress.h" progress::progress(QWidget *parent, Qt::WFlags flags) : QDialog(parent, flags) { ui.setupUi(this); setWindowTitle(tr("Progress")); QGridLayout *layout = new QGridLayout( this ); numLabel = new QLabel(tr("File Num:")); numLineEdit = new QLineEdit; numLineEdit->setText(tr("10")); typeLabel = new QLabel(tr("Progress Type:")); typeComboBox = new QComboBox; typeComboBox->addItem(tr("ProgressBar")); typeComboBox->addItem(tr("ProgressDialog")); progressBar = new QProgressBar; startPushButton = new QPushButton(tr("Start")); layout->addWidget( numLabel, 0, 0 ); layout->addWidget( numLineEdit, 0, 1 ); layout->addWidget( typeLabel, 1, 0 ); layout->addWidget( typeComboBox, 1, 1 ); layout->addWidget( progressBar, 2, 0, 1, 2 ); layout->addWidget( startPushButton, 3, 1 ); layout->setMargin(15); layout->setSpacing(10); connect(startPushButton,SIGNAL(clicked()),this,SLOT(slotStart())); } progress::~progress() { } void progress::slotStart() { int num=numLineEdit->text().toInt(); if(typeComboBox->currentIndex() == 0)/*ProgressBar*/ { progressBar->setRange(0,num); for (int i=1; i<num+1; i++) { progressBar->setValue(i); /*此处模拟文件拷贝过程*/ QTest::qWait(1000); } } else if(typeComboBox->currentIndex() == 1)/*modal ProgressDialog*/ { QProgressDialog *progressDialog = new QProgressDialog(this); progressDialog->setWindowModality(Qt::WindowModal); progressDialog->setMinimumDuration(5); progressDialog->setWindowTitle(tr("Please Wait")); progressDialog->setLabelText(tr("Copying...")); progressDialog->setCancelButtonText(tr("Cancel")); progressDialog->setRange(0,num); for (int i=1; i<num+1; i++) { progressDialog->setValue(i); qApp->processEvents(); /*此处模拟文件拷贝过程*/ QTest::qWait(1000); if (progressDialog->wasCanceled()) return; } } }
main.cpp
#include "progress.h" #include <QtGui/QApplication> int main(int argc, char *argv[]) { QApplication a(argc, argv); progress *w=new progress; w->show(); return a.exec(); }
==============================================================================
本博客已经废弃,不在维护。新博客地址:http://wenchao.ren
我喜欢程序员,他们单纯、固执、容易体会到成就感;面对压力,能够挑灯夜战不眠不休;面对困难,能够迎难而上挑战自我。他
们也会感到困惑与傍徨,但每个程序员的心中都有一个比尔盖茨或是乔布斯的梦想“用智慧开创属于自己的事业”。我想说的是,其
实我是一个程序员
==============================================================================