Qt 程序启动画面


程序启动画面一般用于显示软件信息(名称、作者、版权等)以及减少程序加载过程中的枯燥感。

在Qt中,可以通过QSplashScreen类来为应用程序添加一个启动画面,它会在应用程序的主窗口出现前显示一个图片,并且可以在图片上显示想要输出的信息。 

下面是一个简单的例子:

  1. #include <QApplication>   
  2. #include <QTextEdit>   
  3. #include <QSplashScreen>   
  4. #include <QtTest>   
  5. int main(int argc, char *argv[])  
  6. {  
  7.     QApplication app(argc, argv);  
  8.     QSplashScreen *splash = new QSplashScreen;  
  9.     splash->setPixmap(QPixmap(":/images/splash.png"));  
  10.     splash->show();  
  11.     Qt::Alignment topRight = Qt::AlignRight | Qt::AlignTop;  
  12.     splash->showMessage(QObject::tr("Setting up the main Window..."),  
  13.                         topRight,  
  14.                         Qt::red);  
  15.     QTest::qSleep(3000);  
  16.     QTextEdit *textEdit = new QTextEdit;  
  17.     splash->showMessage(QObject::tr("Loading modules..."),  
  18.                         topRight,  
  19.                         Qt::blue);  
  20.     QTest::qSleep(3000);  
  21.     textEdit->show();  
  22.     splash->finish(textEdit);  
  23.     delete splash;  
  24.     return app.exec();  
  25. }  

注意1:

启动画面图片是通过setPixmap()来指定的,在这里图片是一个资源,因此,需要把图片添加到资源文件(.qrc)中;否则,看不到启动画面。 

注意2:

在例子程序中,使用了QTest::qSleep()函数,因此,需要包含头文件<QTest>,并在.pro文件中,加入

        CONFIG += qtestlib 

最终效果如下:

 

 

相关内容