QT中進(jìn)度對(duì)話框(QProgressDialog)
考慮程序的友好性,當(dāng)程序在執(zhí)行一項(xiàng)耗時(shí)操作時(shí),界面應(yīng)告訴用戶“程序還在運(yùn)行中”,那么,QT中進(jìn)度對(duì)話框(QProgressDialog)可以滿足要求。
(1) 如果所耗總時(shí)間已知,則不需要借助定時(shí)器。
????QProgressDialog?dialog(tr("文件復(fù)制進(jìn)度"),?tr("取消"),?0,?50000,?this); ????dialog.setWindowTitle(tr("進(jìn)度對(duì)話框")); ????dialog.setWindowModality(Qt::WindowModal); ????dialog.show(); ????for(int?i?=?0;?i?<?50000;?i++)//已知最大值不超過(guò)50000 ????{ ????????dialog.setValue(i); ????????QCoreApplication::processEvents(); ????????if(dialog.wasCanceled()) ????????????break; ????} ????dialog.setValue(50000); ????qDebug()<<tr("復(fù)制結(jié)束!");
12345678910111213141234567891011121314
(2) 如果所耗總時(shí)間無(wú)法估計(jì),則需要借助定時(shí)器, 做一個(gè)“沒(méi)有終點(diǎn)”的進(jìn)度對(duì)話框。?
詳細(xì)源代碼請(qǐng)參考,不需要積分喔:?
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);//開(kāi)啟一個(gè)沒(méi)有終點(diǎn)的定時(shí)器 //執(zhí)行耗時(shí)操作。。。 //耗時(shí)操作完成后,關(guān)閉進(jìn)度對(duì)話框 ??timer->stop();//停止定時(shí)器 ??if(currentValue?!=?100) ??????currentValue?=?100; ??progDlg->setValue(currentValue);//進(jìn)度達(dá)到最大值 ??delete?progDlg;//關(guān)閉進(jìn)度對(duì)話框 //借助定時(shí)器,不斷更新進(jìn)度條,直到耗時(shí)操縱結(jié)束 void?updateProgressDialog() { ????currentValue++;?? ????if(?currentValue?==?100?)?? ????????currentValue?=?0;?? ????progDlg?->setValue(currentValue); ????QCoreApplication::processEvents();//避免界面凍結(jié) ????if(progDlg->wasCanceled()) ????????progDlg->setHidden(true);//隱藏對(duì)話框 }
12345678910111213141516171819202122232425262728293031321234567891011121314151617181920212223242526272829303132
QT進(jìn)度對(duì)話框(QProgressDialog)運(yùn)行效果圖: