qt 启动画面显示图片_QSplashScreen类实现Qt程序启动画面

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

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

下面是一个简单的例子:

#include 

#include 

#include 

#include 

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()函数,因此,需要包含头文件,并在.pro文件中,加入

CONFIG += qtestlib

最终效果如下:

1. [代码]cpp代码

01

#include

02

#include

03

#include

04

#include "ui_browser.h"

05

06

int main(int argc,char **argv)

07

{

08

QApplication app(argc, argv);

09

10

QPixmap pixmap("splash.png");

11

QSplashScreen *splash =new QSplashScreen(pixmap);

12

splash->show();

13

14

QMainWindow *form =new QMainWindow;

15

Ui::MainWindow ui;

16

ui.setupUi(form);

17

ui.textBrowser->setSource(QString("files:///C:/Qt/4.1.2/doc/html/index.html"));

18

form->show();

19

20

splash->finish(form);

21

delete splash;

22

23

return app.exec();

24

}

2. [代码]而采用计时器来控制显示时间的话,可用下面方法自己制作SplashWindow。     跳至 [1] [2] [全屏预览]

01

#include

02

#include

03

#include

04

#include "ui_browser.h"

05

06

int main(int argc,char **argv)

07

{

08

QApplication app(argc, argv);

09

10

QDialog dialog;

11

12

QMainWindow *form =new QMainWindow;

13

Ui::MainWindow ui;

14

ui.setupUi(form);

15

ui.textBrowser->setSource(QString("files:///C:/Qt/4.1.2/doc/html/index.html"));

16

17

QTimer timer;

18

QObject::connect(&timer, SIGNAL(timeout()), form, SLOT(show()));

19

QObject::connect(&timer, SIGNAL(timeout()), &dialog, SLOT(accept()));

20

timer.start(10000);

21

dialog.exec();

22

23

return app.exec();

24

}

一个样例程序,往往有一个启动界面一个方面是显得你的程序不那么呆板,同时你的一些初始化过程也可以在这个过程中完成

QT当中提供了:一个类来实现

#include

#include "sortdialog.h"

#include //提供启动画面的类

int main(int argc,char *argv[])

{

QApplication app(argc,argv);

QSplashScreen *splash = new QSplashScreen;

splash->setPixmap(QPixmap("shot.png")); //这里提供在启动时显示的画面

splash->show();

Qt::Alignment topRight = Qt::AlignRight | Qt::AlignTop;

splash ->showMessage(QObject::tr("Setting up the main window....."),topRight,Qt::red);

// while(1);

for(int i=0;i<1000;i++)

{

splash->repaint();

}//这里做一个等待,如果有设置程序可以写在这里

SortDialog *dialog = new SortDialog;  //这里生成主程序

splash -> showMessage(QObject::tr("Loading modules..."),topRight,Qt::red);

//loadModules();

for(int i=0;i<1000;i++)

{

splash->repaint();

}

splash ->showMessage(QObject::tr("Establishing connecting....."),topRight,Qt::red);

//establishConnections();

dialog ->setColumnRange(‘C‘,‘F‘);

for(int i=0;i<1000;i++)

{

splash->repaint();

}

splash->finish(dialog);

dialog -> show();

delete splash;//删掉,回收内存

return app.exec();

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值