当前位置:C++技术网 > 资讯 > 在QT中实现软件启动画面的效果

在QT中实现软件启动画面的效果

更新时间:2016-02-08 21:43:59浏览次数:1+次

     很多时候,软件在完全启动之前要做一些准备,这个时候显示一个画面,告诉用户软件在干啥,让用户知道软件启动了,不是死机了。
     Qt中有现成的Api共我们使用,实现启动画面要用的头文件是
               #include<QSplashScreen>
这个头文件就包括了我们创建启动画面所需要的东西。
首先,创建一个启动画面
    QSplashScreen* splash = new QSplashScreen;
然后设置好启动图片
 splash->setPixmap(QPixmap(":/images/start.png"));
    splash->show();
通常启动画面不仅仅只是图片,我们还需要在启动画面上面显示一些信息,用qt提供的api也可以简单实现:
 Qt::Alignment topRight = Qt::AlignRight|Qt::AlignTop;
    splash->showMessage("正在启动中",topRight,Qt::white);
画面正常显示后等待程序加载,在加载完之后,就可以删除启动画面了。
 splash->finish(&window);
        delete splash;
   就这样简单实现了软件启动画面。
部分代码:
#include<QApplication>
#include "mainwindows.h"
#include<QSplashScreen>
#include<QPixmap>
int main(int argc,char*argv[])
{
 QApplication app(argc,argv);
 QSplashScreen* splash = new QSplashScreen;
 splash->setPixmap(QPixmap(":/images/start.png"));
 splash->show();
 Qt::Alignment topRight = Qt::AlignRight|Qt::AlignTop;
 splash->showMessage("正在启动中",topRight,Qt::white);

 mainWindows window;
 window.show();
 splash->finish(&window);
 delete splash;
 return app.exec();
}

效果如下: