纵所周之,当一个程序的启动比较耗时的时候,为了不让用户枯燥的等待或者是误以为程序运行异常了,所以我们都会在启动比较耗时的程序中加上启动界面

,例如office软件等等。

在Qt中实现启动界面,主要就是使用QSplashScreen类。该类比较简单,这里就不对类本身做过多说明了,主要是以一个例子来说明他的使用方法。

1、首先,我们的实现如下:

  1. #include <QApplication>
  2. #include <QSplashScreen>
  3. #include <QPixmap>
  4. #include <mainwindow.h>
  5. #include <QDebug>
  6. int main(int argc, char *argv[])
  7. {
  8. QApplication app(argc, argv);
  9. QPixmap pixmap("screen.png");
  10. QSplashScreen screen(pixmap);
  11. screen.show();
  12. screen.showMessage("LOVE", Qt::AlignCenter, Qt::red);
  13. MainWindow window;
  14. window.show();
  15. screen.finish(&window);
  16. return app.exec();
  17. }

这个时候运行程序,发现确实出现了启动界面,但是启动界面一闪而过,根本没啥作用。

2、然后,我们想到的就是是否可以加个延时,使得Mainwindow初始化和启动界面之间的时间久一点呢?

下面是我们的第二段代码:

  1. #include <QApplication>
  2. #include <QSplashScreen>
  3. #include <QPixmap>
  4. #include <mainwindow.h>
  5. #include <QDateTime>#include <QDebug>
  6. int main(int argc, char *argv[])
  7. {
  8. QApplication app(argc, argv);
  9. QPixmap pixmap("screen.png");
  10. QSplashScreen screen(pixmap);
  11. screen.show();
  12. app.processEvents();
  13. screen.showMessage("LOVE");
  14. QDateTime n=QDateTime::currentDateTime();
  15. QDateTime now;
  16. do{
  17. now=QDateTime::currentDateTime();
  18. } while (n.secsTo(now)<=5);//6为需要延时的秒数
  19. MainWindow window;
  20. window.show();
  21. screen.finish(&window);
  22. return app.exec();
  23. }

运行的结果依然是一闪而过,而且感觉加的延时不是启动画面的延时,而是程序开始运行的延时,也就是说过了5秒才显示启动画面,然后启动画面一闪而过,最后显示Mainwindow。

插曲:这个时候,我就开始在网上查资料了,看到网上也是说要加延时,而且一定要加上“ app.processEvents(); ”这句话,不然程序睡眠,界面不会得到更新。

具体的就是参考下面两个文章。最终我才知道我调用事件处理的地方不对,应该是在延时的5s之内都调用的,不然这5s之内界面也不会更新。

http://blog.csdn.net/dbzhang800/article/details/6300425

http://mobile.51cto.com/hot-238884.htm

3、有了上面的分析之后,下面两个做法都是可以的

(1),在启动画面和Mwindow构造之前延时,且延时之间调用事件处理,代码如下,打开任意一个if 0,就可以了:

  1. #include <QApplication>
  2. #include <QSplashScreen>
  3. #include <QPixmap>
  4. #include <mainwindow.h>
  5. #include <QDebug>
  6. #include <QElapsedTimer>
  7. #include <QDateTime>
  8. int main(int argc, char *argv[])
  9. {
  10. QApplication app(argc, argv);
  11. QPixmap pixmap("screen.png");
  12. QSplashScreen screen(pixmap);
  13. screen.show();
  14. screen.showMessage("LOVE", Qt::AlignCenter, Qt::red);
  15. #if 0
  16. int delayTime = 5;
  17. QElapsedTimer timer;
  18. timer.start();
  19. while(timer.elapsed() < (delayTime * 1000))
  20. {
  21. app.processEvents();
  22. }
  23. #endif
  24. #if 0
  25. QDateTime n=QDateTime::currentDateTime();
  26. QDateTime now;
  27. do{
  28. now=QDateTime::currentDateTime();
  29. app.processEvents();
  30. } while (n.secsTo(now)<=5);//5为需要延时的秒数
  31. #endif
  32. MainWindow window;
  33. window.show();
  34. screen.finish(&window);
  35. return app.exec();
  36. }

(2)    在Mainwindow构造函数之中延时并且调用事件处理,这个跟QSplashScreen类中的finish()方法相关,不懂的可以看看说明。代码如下:

  1. #include "mainwindow.h"
  2. #include <QTextEdit>
  3. #include <QDateTime>
  4. #include <QCoreApplication>
  5. MainWindow::MainWindow(QWidget *parent) :
  6. QMainWindow(parent)
  7. {
  8. QTextEdit *edit=new QTextEdit;
  9. edit->setText("Splash Example!");
  10. setCentralWidget(edit);
  11. resize(600,450);
  12. QDateTime n=QDateTime::currentDateTime();
  13. QDateTime now;
  14. do{
  15. now=QDateTime::currentDateTime();
  16. QCoreApplication::processEvents();
  17. } while (n.secsTo(now)<=5);//5为需要延时的秒数
  18. }

运行效果跟预期一样,截图如下:

http://blog.csdn.net/chenlong12580/article/details/23713025

Qt中实现启动画面(延时过程中要加上app.processEvents())的更多相关文章

  1. Qt中实现启动画面

    纵所周之,当一个程序的启动比较耗时的时候,为了不让用户枯燥的等待或者是误以为程序运行异常了,所以我们都会在启动比较耗时的程序中加上启动界面 ,例如office软件等等. 在Qt中实现启动界面,主要就是 ...

  2. 在iOS App 中添加启动画面

    你可以认为你需要为启动画面编写代码,然而Apple 让你可以非常简单地在Xcode中完成.不需要编写代码,你仅需要在Xcode中进行一些配置. 1.什么是启动画面(Splash Screen)? 启动 ...

  3. WPF中使用WindowChrome美化窗口过程中的一个小问题

    WPF中使用WindowChrome美化窗口,在园子里有几篇不错的文章,我也是参考练习过程中发现的问题,并记录下来. 在看过几篇教程后,给出的窗口很多出现这样一个问题,如果设置了窗口标题栏的高度大于默 ...

  4. Java中在时间戳计算的过程中遇到的数据溢出问题

    背景 今天在跑定时任务的过程中,发现有一个任务在设置数据的查询时间范围异常,出现了开始时间戳比结束时间戳大的奇怪现象,计算时间戳的代码大致如下. package com.lingyejun.authe ...

  5. Android中的ListView的绘制过程中执行的方法

    首先,系统在绘制ListView之前, 将会先调用getCount方法来获取Item的个数.(如果getCount方法返回0的话,列表时不显示任何内容的) 之后每绘制一个 Item就会调用一次getV ...

  6. 延时过程中要加上app.processEvents(),进度条里也要加上这句

    如何让程序等待一段时间QTime t;t.start();while(t.elapsed()<1000);这种死循环也是一种常见错误用法.但改成正确的还是比较简单的: QTime t;t.sta ...

  7. Microsoft Visual Studio(VS)启动报安装过程中无法运行

    开机启动VS提示无法运行,很可能VS正在更新,可以等待几分钟更新完成,再次运行VS. 也可以把更新进程结束,进程名:VSIXAutoUpdate.exe

  8. qt qsplashscreen 启动画面 延时

    intdelayTime=3; QElapsedTimer timer; timer.start(); while(timer.elapsed()<(delayTime*1000)) { app ...

  9. Python ctypes 在 Python 2 和 Python 3 中的不同 // 使用ctypes过程中问题汇总

    In Python 2.7, strings are byte-strings by default. In Python 3.x, they are unicode by default. Try ...

随机推荐

  1. android run process

    http://www.jb51.net/article/32798.htm http://www.elecfans.com/tongxin/119/20120315263977.html 图 1 详细 ...

  2. BZOJ 1017 魔兽地图DotR(树形DP)

    题意:有两类装备,高级装备A和基础装备B.现在有m的钱.每种B有一个单价和可以购买的数量上限.每个Ai可以由Ci种其他物品合成,给出Ci种其他物品每种需要的数量.每个装备有一个贡献值.求最大的贡献值. ...

  3. LeetCode_Pascal's Triangle II

    Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3 ...

  4. PowerShell_零基础自学课程_5_自定义PowerShell环境及Powershell中的基本概念

    PowerShell_零基础自学课程_5_自定义PowerShell环境及Powershell中的基本概念 据我个人所知,windows下的cmd shell除了能够通过修改系统参数来对其中的环境变量 ...

  5. Android开源中国客户端学习 (自定义View)左右滑动控件ScrollLayout

    左右滑动的控件我们使用的也是非常多了,但是基本上都是使用的viewpager 等 android基础的控件,那么我们有么有考虑过查看他的源码进行定制呢?当然,如果你自我感觉非常好的话可以自己定制一个, ...

  6. mysql use mysql hang

    uat-db03:/root# mysql -A -uroot -p1234567 Warning: Using a password on the command line interface ca ...

  7. 【转】Android源码学习(2)使用Git和Repo进行版本管理

    原文网址:http://blog.chinaunix.net/uid-26074270-id-2458828.html Android项目采用Git和Repo进行版本管理.在大多数情况下,Git都可以 ...

  8. OpenStackCLI调试及术语识记

    1,Project are organizational units in the cloud,and are also known as tenants or accounts.Each user ...

  9. ios实现程序切入后台,实现后台任务

    首先,iOS 会再持续切入后台,给我们5秒钟的时间去处理相关数据,5秒后,程序不会再执行任何代码,处于挂起状态. // 项目需求,按下Home切换后台后向服务器传一些数据,废话不多说,直接上代码 /* ...

  10. android CMWAP, CMNET有何差别

    什么是CMNET,什么是CMWAP? 答:CMWAP和CMNET仅仅是中国移动为其划分的两个GPRS接入方式.中国移动对CMWAP作了一定的限制,主要表如今CMWAP接入时仅仅能訪问GPRS网络内的I ...