Qt 程序启动画面
Qt 程序启动画面
程序启动画面一般用于显示软件信息(名称、作者、版权等)以及减少程序加载过程中的枯燥感。
在Qt中,可以通过QSplashScreen类来为应用程序添加一个启动画面,它会在应用程序的主窗口出现前显示一个图片,并且可以在图片上显示想要输出的信息。
下面是一个简单的例子:
- #include <QApplication>
- #include <QTextEdit>
- #include <QSplashScreen>
- #include <QtTest>
- int main(int argc, char *argv[])
- {
- QApplication app(argc, argv);
- QSplashScreen *splash = new QSplashScreen;
- splash->setPixmap(QPixmap(":/images/splash.png"));
- splash->show();
- Qt::Alignment topRight = Qt::AlignRight | Qt::AlignTop;
- splash->showMessage(QObject::tr("Setting up the main Window..."),
- topRight,
- Qt::red);
- QTest::qSleep(3000);
- QTextEdit *textEdit = new QTextEdit;
- splash->showMessage(QObject::tr("Loading modules..."),
- topRight,
- Qt::blue);
- QTest::qSleep(3000);
- textEdit->show();
- splash->finish(textEdit);
- delete splash;
- return app.exec();
- }
注意1:
启动画面图片是通过setPixmap()来指定的,在这里图片是一个资源,因此,需要把图片添加到资源文件(.qrc)中;否则,看不到启动画面。
注意2:
在例子程序中,使用了QTest::qSleep()函数,因此,需要包含头文件<QTest>,并在.pro文件中,加入
CONFIG += qtestlib
最终效果如下:
评论暂时关闭