Qt5启动画面

在QT5中,对于程序的启动界面是通过QSplashScreen来实现的,他会在应用程序的主窗口显示前显示一个图片,在该图片上可以通过文字来显示想要输出的信息。

QSplashScreen在QT中的API如下:

class Q_WIDGETS_EXPORT QSplashScreen : public QWidget
{
    Q_OBJECT
public:
    explicit QSplashScreen(const QPixmap &pixmap = QPixmap(), Qt::WindowFlags f = 0);
    QSplashScreen(QWidget *parent, const QPixmap &pixmap = QPixmap(), Qt::WindowFlags f = 0);
    virtual ~QSplashScreen();

    void setPixmap(const QPixmap &pixmap);
    const QPixmap pixmap() const;
    void finish(QWidget *w);
    void repaint();
    QString message() const;

public Q_SLOTS:
    void showMessage(const QString &message, int alignment = Qt::AlignLeft,
                  const QColor &color = Qt::black);
    void clearMessage();

Q_SIGNALS:
    void messageChanged(const QString &message);

protected:
    bool event(QEvent *e);
    virtual void drawContents(QPainter *painter);
    void mousePressEvent(QMouseEvent *);

private:
    Q_DISABLE_COPY(QSplashScreen)
    Q_DECLARE_PRIVATE(QSplashScreen)
};

 其中最常用的的几个函数API如下:

      void showMessage(const QString &message, int alignment = Qt::AlignLeft, const QColor &color = Qt::black);//设置图片信息,message为显示的文本,alignment为对齐方式,color为文本颜色

void finish(QWidget *w);//启动画面结束

void setPixmap(const QPixmap &pixmap); //设置启动画面的图片

此时显示一个启动画面的demo如下所示:

#include "mainwindow.h"
#include <QApplication>
#include <QSplashScreen>
#include <QPixmap>

int main(int argc, char *argv[])
{
    QApplication app(argc, argv);
    QPixmap pixmap(":/images/move.png");
    QSplashScreen *SplashScreen = new QSplashScreen;
    SplashScreen->setPixmap(pixmap);//设置启动图片
    SplashScreen->show();//显示

    Qt::Alignment align = Qt::AlignTop | Qt::AlignRight;//右上
    SplashScreen->showMessage("test",align,Qt::red);//设置图片上文本信息

    MainWindow w;
    SplashScreen->finish(&w);
    w.show();

    delete SplashScreen; //回收内存
    return app.exec();
}

     此时,运行程序你会发现的确出现了启动画面,但是这个画面一闪而过,并没有起到太大的作用,因此我们想到延时,使用延时就必须使用processEvents()来处理消息,对于时间上的延迟可以通过QDateTimeQElapsedTimer  来实现。

     1 我们可以通过qint64 secsTo(const QDateTime &) const 来实现时间差,通过时间差来设定延时

     2 我们也可以通过qint64 QElapsedTimer::elapsed() const 来获取QElapsedTimer对象最近一次开始后的时间差,单位为毫秒

   3 我们可以通过QDateTime addSecs(qint64 secs) const先增加一个延时的时间间隔,在两个时间相等的时候就启动主界面

#include "mainwindow.h"
#include <QApplication>
#include <QSplashScreen>
#include <QPixmap>
#include <QDateTime>
#include <QElapsedTimer>

int main(int argc, char *argv[])
{
    QApplication app(argc, argv);
    QPixmap pixmap(":/images/move.gif");
    QSplashScreen *SplashScreen = new QSplashScreen;
    SplashScreen->setPixmap(pixmap);
    SplashScreen->show();

    Qt::Alignment align = Qt::AlignTop | Qt::AlignRight;
    SplashScreen->showMessage("test",align,Qt::red);

    //设置延时
    //第一种方式
    QDateTime curDateTime = QDateTime::currentDateTime();
    QDateTime nowDateTime;
    do
    {
        nowDateTime = QDateTime::currentDateTime();
        app.processEvents();

    }while(curDateTime.secsTo(nowDateTime) <= 5);

    //第二种方式
//    int nWaitTime = 5000;
//    QElapsedTimer timer;
//    timer.start();
//    while(timer.elapsed() < nWaitTime){
//        app.processEvents();
//    }

    //第三种方式
//    QDateTime curDateTime = QDateTime::currentDateTime();
//    QDateTime furDateTime = curDateTime.addSecs(5);
//    while(curDateTime != furDateTime){
//        curDateTime = QDateTime::currentDateTime();
//        app.processEvents();
//    }

    MainWindow w;
    SplashScreen->finish(&w);
    w.show();

    delete SplashScreen;
    return app.exec();
}

 参考博客:http://blog.csdn.net/chenlong12580/article/details/23713025

原文地址:https://www.cnblogs.com/tianzhang/p/4931148.html