转载于:http://blog.csdn.net/learn_sunzhuli/article/details/46289935
考虑程序的友好性,当程序在执行一项耗时操作时,界面应告诉用户“程序还在运行中”,那么,QT中进度对话框(QProgressDialog)可以满足要求。
(1) 如果所耗总时间已知,则不需要借助定时器。
QProgressDialog dialog(tr("文件复制进度"), tr("取消"), 0, 50000, this);
dialog.setWindowTitle(tr("进度对话框"));
dialog.setWindowModality(Qt::WindowModal);
dialog.show();
for(int i = 0; i < 50000; i++)
{
dialog.setValue(i);
QCoreApplication::processEvents();
if(dialog.wasCanceled())
break;
}
dialog.setValue(50000);
qDebug()<<tr("复制结束!");
(2) 如果所耗总时间无法估计,则需要借助定时器, 做一个“没有终点”的进度对话框。
详细源代码请参考,不需要积分喔:
http://download.csdn.net/detail/learn_sunzhuli/8757715
progDlg = new QProgressDialog();
progDlg->setWindowTitle("Please wait...");
progDlg->setFixedWidth(300);
progDlg->setRange(0, 100);
progDlg->show();
timer = new QTimer();
currentValue = 0;
progDlg->setValue(currentValue);
connect(timer, SIGNAL(timeout()), this, SLOT(updateProgressDialog()));
timer->start(100);
timer->stop();
if(currentValue != 100)
currentValue = 100;
progDlg->setValue(currentValue);
delete progDlg;
void updateProgressDialog()
{
currentValue++;
if( currentValue == 100 )
currentValue = 0;
progDlg ->setValue(currentValue);
QCoreApplication::processEvents();
if(progDlg->wasCanceled())
progDlg->setHidden(true);
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
QT进度对话框(QProgressDialog)运行效果图: