程序啟動畫面
有時候程序在啟動時需要較多資源或者有些耗時操作,這時用戶在等待的過程中如果給用戶以啟動界面無疑大大提升了用戶體驗。Qt給程序員提供了一個封裝好的類QSplashScreen,使程序員能較快較好的實現一個程序的啟動畫面功能。實現代碼如下:

1 int main(int argc, char *argv[]) 2 { 3 QApplication a(argc, argv); 4 5 QSplashScreen *splash = new QSplashScreen(); 6 splash->setPixmap(QPixmap("../images/splash.jpg")); 7 splash->show(); 8 //設置啟動界面的顯示信息 9 Qt::Alignment topRight = Qt::AlignLeft | Qt::AlignBottom; //Qt::AlignRight | Qt::AlignTop; 10 splash->showMessage(QString("Loading the main window..."),topRight,Qt::white); 11 QThread::sleep(3); //模擬窗口加載 12 13 database w; 14 splash->showMessage(QString("Loading the modules..."),topRight,Qt::white); 15 QThread::sleep(3); //模擬軟件模塊加載 16 splash->showMessage(QString("Connecting database & servers..."),topRight,Qt::white); 17 QThread::sleep(2); //模擬連接服務器相關 18 splash->finish(&w); 19 delete splash; 20 21 w.show(); 22 return a.exec(); 23 }
幾個sleep()模仿程序啟動時的耗時操作。
ps:Qt中QThread::sleep(3)中的參數是以秒為單位的,此處是sheep3秒!
images文件夾和程序工程文件夾要放在同一目錄下!