很多程序在启动的时候需要加载很多资源,为了避免用户的无聊,都使用启动画面,在程序加载结束后,启动画面消失。在Qt中使用QSplashScreen类可以实现,例如,创建一个基于QMainWindow的程序,代码如下:
/*
Qt实现程序启动画面
*/
#include "StartScreen.h"
#include <QtWidgets/QApplication>
#include <QSplashScreen>
#include <QDateTime>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
// 设置文字显示位置
Qt::Alignment topRight = Qt::AlignBottom | Qt::AlignRight;
QString startPng = qApp->applicationDirPath() + "\\start.png";
//设置启动画面图片
QSplashScreen *m_AppSplash = new QSplashScreen(QPixmap(startPng));
m_AppSplash->show();
//显示文字示例
m_AppSplash->showMessage(QString::fromLocal8Bit("程序启动中,大概6秒"), topRight, Qt::white);
a.processEvents();
QDateTime n = QDateTime::currentDateTime();
QDateTime now;
//设置延时,或者不设置,当主程序加载完显示后,启动画面也会消失
do
{
now = QDateTime::currentDateTime();
}
while (n.secsTo(now) <= 5); //5为需要延时的秒数
StartScreen w;
w.show();
m_AppSplash->finish(&w); //当