Qt设置启动画面和广告画面

来源:互联网 发布:彝族音乐 知乎 编辑:程序博客网 时间:2024/04/29 04:09

Qt之启动画面

       目前的手机应用都是有启动画面的,但是PC端软件的基本没有启动画面,移动端还是比较常见的,在相继开发了几款APP之后,总结一下Qt启动画面的设置方法,也许启动画面的设置方法还有很多,下面总结一下个人所使用的几种方法:

1. TheQSplashScreen widget provides a splash screen that can be shown duringapplication startup.

这个是Qt手册的原文,我们利用Qt提供便捷的启动画面类来实现启动画面,实现启动画面,根据手册提示有几种方式,我只使用了其中的两种。

//2.启动画面

       //2.1一级启动画面

        QPixmap pixmap(":/Resources/start.jpg");  pixmap.scaled(QApplication::desktop()->availableGeometry().size(),Qt::KeepAspectRatio);QSplashScreen m_screen(pixmap);  m_screen.move(0,0);m_screen.setDisabled(true);//禁止界面输入事件m_screen.show();QDateTime n=QDateTime::currentDateTime(); QDateTime now;  do{  now=QDateTime::currentDateTime();  a.processEvents();  } while (n.secsTo(now)<=3);</span>
        ...........m_screen.close();</span>

这是一种方法,main函数中添加即可。

2. QSplashScreensupports this with the showMessage() function. If you wish to do your owndrawing you can get a pointer to the pixmap used in the splash screen withpixmap(). Alternatively, you can subclass QSplashScreen and reimplementdrawContents().

这个是第二中方法原文:

实现过程如下:

我们要子类化QsplashScreen,这样我们就可以将二级启动画面作为广告画面了.

<pre name="code" class="cpp">/*--------------------------------------------------------  @Purpose:实现启动画面  @Time:2016.6.6  @Author:QFGL  ----------------------------------------------------------*/  #include <QSplashScreen>  #include <QLabel>  #include <QString>  #include <QPushButton>class CSplashScreen :  public QSplashScreen  {  Q_OBJECT  public:  explicit CSplashScreen(QPixmap& pixmap,int time);  ~CSplashScreen(void);  private:  //1.加载启动画面  void LoadPhoto();private slots://2.倒计时void CountDown();public:  //倒计时值  int interval;//记录倒计时剩余值qint32 m_Time;public:  //标签类  QLabel  *m_Label;  //二级广告画面显示按钮  QPushButton *m_PushButton;  };
#include "QFSplashScreen.h"  #include <stdlib.h>  #include <windows.h>  #include "stdafx.h"CSplashScreen::CSplashScreen(QPixmap& pixmap,int reducetime) :  QSplashScreen(pixmap),interval(reducetime)  {  m_PushButton = new QPushButton(this);  m_Label= new QLabel(m_PushButton);m_PushButton->setGeometry(pixmap.width()-100,0,100,30);  m_PushButton->setText("SKIP");  m_PushButton->setStyleSheet("QPushButton{color:blue;font:18px;text-align:center;border:none }QPushButton::chunk {background-color: rgba(0, 0, 0,0);}");  m_PushButton->setStyleSheet("QLabel{color:blue;font:30px;text-align:center;}QLabel::chunk {background-color: rgba(0, 0, 0,0);}");  m_Time=3;LoadPhoto();  }  CSplashScreen::~CSplashScreen(void)  {  }  //启动画面  void CSplashScreen:: LoadPhoto()  {   qint32 iTime = interval/2.5;  for (qint32 i = 0; i < 3; i++)  {  QTimer::singleShot(i*iTime,this,SLOT(CountDown()));  }   QTimer::singleShot(interval,this,SLOT(close()));  }  //倒计时void CSplashScreen::CountDown(){QString sTime, m_timeString;m_timeString = sTime.number(m_Time,10);m_Label->setText(m_timeString);m_timeString.clear();m_Time--;}


我们可以将这个子类作为广告画面,这样就有倒计时显示了,但是SKIP大家要自己添加接口,就不再赘述了,具体如何使用就看项目情况了,就简单介绍到这。

注:如果大家要添加GIF广告或者启动画面,就要使用QMovie这个类,具体如何使用可以看一下文档了!下一篇将推出浅谈Qt、MFC之GUI编程架构。

这个就是简单的两种使用方法了,如果有什么错误,请联系QFGL:2120263292


0 0
原创粉丝点击