Qt学习——qt软件启动界面

一般,大型软件的启动须要必定的时间,为了改善用户体验,不少软件如word,Photoshop等都会加载开机界面。linux

如图1所示。windows


图 1  Photoshop的启动界面app

Qt中实现开机界面须要借助QSplashScreen类和QPixmap类来实现。

首先,用QPixmap类的对象用来关联一个图片实体,采用的构造函数为:less

QPixmap::QPixmap (const QString fileName, const char format = 0Qt::ImageConversionFlagsflags =Qt::AutoColor )

其次,用QSplashScreen类的对象用来将关联了图片的QPixmap类对象加载应用程序中。函数

再次,调用QSplashScreen类对象的QSplashScreen::show()方法,显示启动动画。动画

最后,程序启动以后,调用QSplashScreen::finish(QWidget* mainWin)方法关闭启动动画。ui

实现代码以下:spa

#include<QtGui/QApplication>翻译

#include"mainwindow.h"code

intmain(intargc,char*argv[])

{

   QApplicationapp(argc,argv);

   //如下三行用于支持在控件中文字的中文显示,windows平台参数为GB2312,linux平台为utf8

   QTextCodec::setCodecForTr(QTextCodec::codecForName("GB2312"));

   QTextCodec::setCodecForLocale(QTextCodec::codecForName("GB2312"));

   QTextCodec::setCodecForCStrings(QTextCodec::codecForName("GB2312"));

   //加载用户自定义的界面翻译文件

   QTranslatortran;

   tran.load("qt_zh_CN.qm","D:\\QtSDK\\Desktop\\Qt\\4.8.1\\mingw\\translations");

   app.installTranslator(&tran);

   //加载开机动画

   QPixmappixmap("E:\\Qt_study\\lesson2\\Example\\notepad\\Image\\拳皇.jpg");

   QSplashScreensplash(pixmap);

   splash.show();

   //模拟开机动画显示时间

  for(longi=0;i<50000;i++)

       for(longj=0;j<100000;j++);

   MainWindoww;

   w.show();

   splash.finish(&w);            //程序启动后,关闭开机动画

   returnapp.exec();

}