程式啟動畫面一般用於顯示軟體資訊(名稱、作者、著作權等)以及減少程式載入過程中的枯燥感。
在Qt中,可以通過QSplashScreen類來為應用程式添加一個啟動畫面,它會在應用程式的主視窗出現前顯示一個圖片,並且可以在圖片上顯示想要輸出的資訊。
下面是一個簡單的例子:
#include <QApplication><br />#include <QTextEdit><br />#include <QSplashScreen><br />#include <QtTest><br />int main(int argc, char *argv[])<br />{<br /> QApplication app(argc, argv);<br /> QSplashScreen *splash = new QSplashScreen;<br /> splash->setPixmap(QPixmap(":/images/splash.png"));<br /> splash->show();<br /> Qt::Alignment topRight = Qt::AlignRight | Qt::AlignTop;<br /> splash->showMessage(QObject::tr("Setting up the main Window..."),<br /> topRight,<br /> Qt::red);<br /> QTest::qSleep(3000);<br /> QTextEdit *textEdit = new QTextEdit;<br /> splash->showMessage(QObject::tr("Loading modules..."),<br /> topRight,<br /> Qt::blue);<br /> QTest::qSleep(3000);<br /> textEdit->show();<br /> splash->finish(textEdit);<br /> delete splash;<br /> return app.exec();<br />}
注意1:
啟動畫面圖片是通過setPixmap()來指定的,在這裡圖片是一個資源,因此,需要把圖片添加到資源檔(.qrc)中;否則,看不到啟動畫面。
注意2:
在例子程式中,使用了QTest::qSleep()函數,因此,需要包含標頭檔<QTest>,並在.pro檔案中,加入
CONFIG += qtestlib
最終效果如下: