首页 > 技术知识 > 正文

Qt启动界面

软件启动时常常会因为加载很长时间从而导致软件打开要好长时间,而这段时间是没有界面显示的。程序可能启动较长时间后,窗口才会显示出来,用户很容易会抱怨程序响应的慢。为了改善用户体验需要添加启动界面,启动时打开一个界面,等到主界面加载完成后关闭启动界面打开主界面,一般启动界面是一张启动图片。 QT提供了QSplashScreen 类,其使用方法比较简单,下面的例子就是,当程序运行到 splash. show();时,启动界面就会显示出来,当主界面的初始化完成,运行到 splash. finish(& w);时, 主窗口显示出来,并且 启动画面隐藏

int main(int argc, char *argv[]) { QApplication a(argc, argv); QPixmap pixmap(“:/new/prefix1/startUp”); pixmap.scaled(QSize(100,100), Qt::KeepAspectRatio); QSplashScreen splash(pixmap); splash.setDisabled(true); //禁用用户的输入事件响应 splash.show(); MainWindow w; w.show(); splash.finish(&w); return a.exec(); }

启动界面: Qt启动界面 启动完成 Qt启动界面1 加载GIF动图启动界面

QPixmap pixmap(“:/prefix/poweron1.gif”); QSplashScreen splash(pixmap); QMovie *Movie = new QMovie(“:/prefix/poweron1.gif”); QLabel *Label = new QLabel(&splash); Label->setMovie(Movie); Movie->start(); splash.show(); for(int i=0; i<5000; i+=Movie->speed()) { QCoreApplication::processEvents(); usleep(500*Movie->speed()); } MainWindow w; w.show(); splash.finish(w);

当程序启动慢的时候,该画面就会停留一会,也可以在图片上显示一下程序启动的加载信息,但是如果程序启动很快的话,启动图片就会一闪而过,其实启动画面也出现了的,只是出现的时间比较短,如果想让程序启动之前画面多停留一下,可以添加一个延时器。

QDateTime n=QDateTime::currentDateTime(); QDateTime now; do{ n now=QDateTime::currentDateTime(); } while (n.secsTo(now)<=10);//10为需要延时的秒数

这样启动画面就可以多停留10秒。

猜你喜欢