QSplashScreen类实现Qt程序启动画面
收藏人:zwsj    
2013-09-13 | 阅:569  转:6  
 |   来源
  |  分享 
 
 
 
  
 
 

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

在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

最终效果如下:

代码片段(2)[全屏查看所有代码]

1. [代码]cpp代码

01 #include <QtGui/QtGui>
02 #include <QtGui/QPixmap>
03 #include <QtGui/QSplashScreen>
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 <QtGui/QtGui>
02 #include <QtGui/QDialog>
03 #include <QtCore/QTimer>
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();
}

 
 

QSplashScreen类实现Qt程序启动画面的更多相关文章

  1. Qt程序启动画面播放(gif与swf两种动画格式)

    学习Qt有一段时间了,发现一个小问题,网上关于Qt的资料或者总结性的学习及应用文章有点少. 比如,Qt完整的API,程序运行之前的启动画面如何按理想效果播放等,每次想在项目中添加一些应用的时候,总是找 ...

  2. qt程序启动画面

  3. QT之实现程序启动画面

    现在好多软件都有,尤其是大型软件,他们要加载的东西太多,因此需要的事件较长,而又不希望给用户产生成为好像没有响应的情况,因此也就需要程序启动画面来实现. 当然了,用widget要比这个丰富很多,不过代 ...

  4. 用VC制作应用程序启动画面

    摘 要:本文提供了四种启动画面制作方法. 使用启动画面一是可以减少等待程序加载过程中的枯燥感(尤其是一些大型程序):二是 可以用来显示软件名称和版权等提示信息.怎样使用VC++制作应用程序的启动画面呢 ...

  5. MFC之窗体改动工具栏编程状态栏编程程序启动画面

    1窗体外观的改动 (1)改动在CMainFrame::preCreateWindow(CREATESTRUCT& cs) 改动标题:cs.style&=FWS_ADDTOTITLE; ...

  6. qt程序启动播放动画

    qt程序启动播放动画 编辑删除转载 2016-01-20 10:23:11 标签:qt启动动画 1.播放动画 QAxWidget *flash = , ); //QAxWidget使用的是Active ...

  7. 嵌入式Qt程序启动参数-qws 不需要X11桌面系统

    1 背景 通过串口终端启动arm开发板(linux系统)的Qt应用程序,提示: [root@FORLINX6410]# /opt/qt-4.7.1/demos/textedit/textedit s3 ...

  8. C# 程序内的类数量对程序启动的影响

    原文:C# 程序内的类数量对程序启动的影响 版权声明:博客已迁移到 http://lindexi.gitee.io 欢迎访问.如果当前博客图片看不到,请到 http://lindexi.gitee.i ...

  9. 2018-10-31-C#-程序内的类数量对程序启动的影响

    title author date CreateTime categories C# 程序内的类数量对程序启动的影响 lindexi 2018-10-31 14:7:6 +0800 2018-10-1 ...

随机推荐

  1. Test for open live write

    this is test document. this is test document. this is test document. this is test document. this is ...

  2. 日期相关---SimpleDateFormat的setLenient(true/false)-----自动计算日期

    有时候我们需要判断用户的日期格式是否正确, 虽然绝大多数会在前台处理,但是也有需要从文件流读入的情况,如果日期不合格就需要抛异常,这时候就需要禁止SimpleDateFormat的自动计算功能. 这时 ...

  3. tyvj 1153 间谍网络 tarjan有向图强连通

    P1153 - 间谍网络 From ForeverBell    Normal (OI)总时限:13s    内存限制:128MB    代码长度限制:64KB 描述 Description 由于外国 ...

  4. uva 1203 - Argus

    简单的优先队列的应用: 代码: #include<queue> #include<cstdio> using namespace std; struct node { int ...

  5. Spring Batch系列总括(转载)

    最近一个项目在使用SpringBatch框架做一个电子商务平台的批处理.网上资料很有限,尤其是中文资料更是少之又少,官网上的文档也只是讲一些入门的基础知识,大部分高级特性都是一笔带过,讲解的很不彻底, ...

  6. LeetCode 面试:Add Binary

    1 题目 Given two binary strings, return their sum (also a binary string). For example,a = "11&quo ...

  7. 《Effective C++》条款14 总是让base class拥有virtual destructor

    有时,一个类想跟踪它有多少个对象存在.一个简单的方法是创建一个静态类成员来统计对象的个数.这个成员被初始化为0,在构造函数里加1,析构函数里减1.(条款m26里说明了如何把这种方法封装起来以便很容易地 ...

  8. group by 替代distinct

    SQL> select distinct employee_id,first_name from test1; 107 rows selected. Execution Plan ------- ...

  9. 【HDOJ】1814 Peaceful Commission

    2-SAT基础题目. /* 1814 */ #include <iostream> #include <vector> #include <algorithm> # ...

  10. 【HDOJ】1811 Rank of Tetris

    并查集+拓扑排序.使用并查集解决a = b的情况. #include <iostream> #include <cstdio> #include <cstring> ...