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. Ubuntu 在右键快捷菜单中添加“Open in Terminal”

    操作步骤翻译如下: 1.打开一个Terminal(ctrl+alt+t),输入如下指令 sudo apt-get install nautilus-open-terminal 2.使用以下指令来重启N ...

  2. AngularJS: 自定义指令与控制器数据交互

    <!doctype html> <html> <head> <meta charset="utf-8"> <title> ...

  3. 编程框架—Autofac

    Autofac是一款轻量级的IOC框架,性能高. Autofac基本使用步骤: 1.创建容器建造者(Builder): 2.对Builder注册类型. 3.Buildder创建容器(Container ...

  4. 同步的HTTP请求

    代码: #import <Foundation/Foundation.h> void request(NSString *urlString) { NSLog(@"BEGIN&q ...

  5. CSS样式的优先级

    1.相同权值情况下,CSS样式的优先级总结来说,就是--就近原则(离被设置元素越近优先级别越高): 内联样式表(标签内部)> 嵌入样式表(当前文件中)> 外部样式表(外部文件中). 2.权 ...

  6. bzoj 2594: [Wc2006]水管局长数据加强版 动态树

    2594: [Wc2006]水管局长数据加强版 Time Limit: 25 Sec  Memory Limit: 128 MBSubmit: 934  Solved: 291[Submit][Sta ...

  7. Codeforces 712E Memory and Casinos

    Description There are n casinos lined in a row. If Memory plays at casino \(i\), he has probability ...

  8. vim下如何删除某行之后的所有行

    使用dG进行删除 在命令模式下将光标置于要删除的起始行,然后依次输入d,G

  9. Jacob - Outlook

    import com.jacob.activeX.ActiveXComponent; import com.jacob.com.Dispatch; public class Util { public ...

  10. 折腾iPhone的生活——我的越狱插件精品筛选

    威锋上有人说的好,iOS系统越狱是为了装更多东西,安卓Root是为了删更多东西.   插件 众所周知,iOS系统是非常封闭的,基本上涉及到底层的功能在iOS上都不能实现,除非越狱装插件,所以插件就成为 ...