程序启动画面
有时候程序在启动时需要较多资源或者有些耗时操作,这时用户在等待的过程中如果给用户以启动界面无疑大大提升了用户体验。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文件夹和程序工程文件夹要放在同一目录下!