您的位置:首页 > 编程语言 > Qt开发

Qt中实现启动画面

2017-07-28 11:50 375 查看
纵所周之,当一个程序的启动比较耗时的时候,为了不让用户枯燥的等待或者是误以为程序运行异常了,所以我们都会在启动比较耗时的程序中加上启动界面

,例如office软件等等。

         在Qt中实现启动界面,主要就是使用QSplashScreen类。该类比较简单,这里就不对类本身做过多说明了,主要是以一个例子来说明他的使用方法。

1、首先,我们的实现如下:

[cpp] view
plain copy

#include <QApplication>  

#include <QSplashScreen>  

#include <QPixmap>  

#include <mainwindow.h>  

#include <QDebug>  

  

int main(int argc, char *argv[])  

{  

    QApplication app(argc, argv);  

  

    QPixmap pixmap("screen.png");  

    QSplashScreen screen(pixmap);  

    screen.show();  

    screen.showMessage("LOVE", Qt::AlignCenter, Qt::red);  

  

    MainWindow window;  

    window.show();  

  

    screen.finish(&window);  

  

    return app.exec();  

}  

这个时候运行程序,发现确实出现了启动界面,但是启动界面一闪而过,根本没啥作用。

2、然后,我们想到的就是是否可以加个延时,使得Mainwindow初始化和启动界面之间的时间久一点呢?

下面是我们的第二段代码:

[cpp] view
plain copy

#include <QApplication>  

#include <QSplashScreen>  

#include <QPixmap>  

#include <mainwindow.h>  

#include <QDateTime>#include <QDebug>  

int main(int argc, char *argv[])  

{      

    QApplication app(argc, argv);  

    QPixmap pixmap("screen.png");      

    QSplashScreen screen(pixmap);      

    screen.show();      

    app.processEvents();      

    screen.showMessage("LOVE");  

  

    QDateTime n=QDateTime::currentDateTime();  

    QDateTime now;      

    do{      

        now=QDateTime::currentDateTime();      

    } while (n.secsTo(now)<=5);//6为需要延时的秒数  

  

  

    MainWindow window;      

    window.show();  

    screen.finish(&window);  

    return app.exec();  

}  

运行的结果依然是一闪而过,而且感觉加的延时不是启动画面的延时,而是程序开始运行的延时,也就是说过了5秒才显示启动画面,然后启动画面一闪而过,最后显示Mainwindow。

插曲:这个时候,我就开始在网上查资料了,看到网上也是说要加延时,而且一定要加上“ app.processEvents(); ”这句话,不然程序睡眠,界面不会得到更新。

             具体的就是参考下面两个文章。最终我才知道我调用事件处理的地方不对,应该是在延时的5s之内都调用的,不然这5s之内界面也不会更新。

            http://blog.csdn.net/dbzhang800/article/details/6300425

            http://mobile.51cto.com/hot-238884.htm

3、有了上面的分析之后,下面两个做法都是可以的

    (1),在启动画面和Mwindow构造之前延时,且延时之间调用事件处理,代码如下,打开任意一个if 0,就可以了:

[cpp] view
plain copy

#include <QApplication>  

#include <QSplashScreen>  

#include <QPixmap>  

#include <mainwindow.h>  

#include <QDebug>  

#include <QElapsedTimer>  

#include <QDateTime>  

  

int main(int argc, char *argv[])  

{  

    QApplication app(argc, argv);  

  

    QPixmap pixmap("screen.png");  

    QSplashScreen screen(pixmap);  

    screen.show();  

    screen.showMessage("LOVE", Qt::AlignCenter, Qt::red);  

#if 0  

    int delayTime = 5;  

    QElapsedTimer timer;  

    timer.start();  

    while(timer.elapsed() < (delayTime * 1000))  

    {  

         app.processEvents();  

    }  

#endif  

  

#if 0  

    QDateTime n=QDateTime::currentDateTime();  

    QDateTime now;  

    do{  

        now=QDateTime::currentDateTime();  

        app.processEvents();  

    } while (n.secsTo(now)<=5);//5为需要延时的秒数  

#endif  

  

    MainWindow window;  

    window.show();  

  

    screen.finish(&window);  

  

    return app.exec();  

}  

    (2)    在Mainwindow构造函数之中延时并且调用事件处理,这个跟QSplashScreen类中的finish()方法相关,不懂的可以看看说明。代码如下:

[cpp] view
plain copy

#include "mainwindow.h"  

  

#include <QTextEdit>  

#include <QDateTime>  

#include <QCoreApplication>  

  

MainWindow::MainWindow(QWidget *parent) :  

    QMainWindow(parent)  

{  

    QTextEdit *edit=new QTextEdit;  

    edit->setText("Splash Example!");  

    setCentralWidget(edit);  

    resize(600,450);  

  

    QDateTime n=QDateTime::currentDateTime();  

    QDateTime now;  

    do{  

        now=QDateTime::currentDateTime();  

        QCoreApplication::processEvents();  

    } while (n.secsTo(now)<=5);//5为需要延时的秒数  

}  

运行效果跟预期一样,截图如下:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: