博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Qt5启动画面
阅读量:4646 次
发布时间:2019-06-09

本文共 3543 字,大约阅读时间需要 11 分钟。

在QT5中,对于程序的启动界面是通过QSplashScreen来实现的,他会在应用程序的主窗口显示前显示一个图片,在该图片上可以通过文字来显示想要输出的信息。

QSplashScreen在QT中的API如下:

 

class Q_WIDGETS_EXPORT QSplashScreen : public QWidget{    Q_OBJECTpublic:    explicit QSplashScreen(const QPixmap &pixmap = QPixmap(), Qt::WindowFlags f = 0);    QSplashScreen(QWidget *parent, const QPixmap &pixmap = QPixmap(), Qt::WindowFlags f = 0);    virtual ~QSplashScreen();    void setPixmap(const QPixmap &pixmap);    const QPixmap pixmap() const;    void finish(QWidget *w);    void repaint();    QString message() const;public Q_SLOTS:    void showMessage(const QString &message, int alignment = Qt::AlignLeft,                  const QColor &color = Qt::black);    void clearMessage();Q_SIGNALS:    void messageChanged(const QString &message);protected:    bool event(QEvent *e);    virtual void drawContents(QPainter *painter);    void mousePressEvent(QMouseEvent *);private:    Q_DISABLE_COPY(QSplashScreen)    Q_DECLARE_PRIVATE(QSplashScreen)};

 

 其中最常用的的几个函数API如下:

      void showMessage(const QString &message, int alignment = Qt::AlignLeft, const QColor &color = Qt::black);//设置图片信息,message为显示的文本,alignment为对齐方式,color为文本颜色

void finish(QWidget *w);//启动画面结束

void setPixmap(const QPixmap &pixmap); //设置启动画面的图片

此时显示一个启动画面的demo如下所示:

 

#include "mainwindow.h"#include 
#include
#include
int main(int argc, char *argv[]){ QApplication app(argc, argv); QPixmap pixmap(":/images/move.png"); QSplashScreen *SplashScreen = new QSplashScreen; SplashScreen->setPixmap(pixmap);//设置启动图片 SplashScreen->show();//显示 Qt::Alignment align = Qt::AlignTop | Qt::AlignRight;//右上 SplashScreen->showMessage("test",align,Qt::red);//设置图片上文本信息 MainWindow w; SplashScreen->finish(&w); w.show(); delete SplashScreen; //回收内存 return app.exec();}

 

     此时,运行程序你会发现的确出现了启动画面,但是这个画面一闪而过,并没有起到太大的作用,因此我们想到延时,使用延时就必须使用processEvents()来处理消息,对于时间上的延迟可以通过QDateTimeQElapsedTimer  来实现。

     1 我们可以通过qint64 secsTo(const QDateTime &) const 来实现时间差,通过时间差来设定延时

     2 我们也可以通过 QElapsedTimer::elapsed() const 来获取QElapsedTimer对象最近一次开始后的时间差,单位为毫秒

   3 我们可以通过QDateTime addSecs(qint64 secs) const先增加一个延时的时间间隔,在两个时间相等的时候就启动主界面

 

#include "mainwindow.h"#include 
#include
#include
#include
#include
int main(int argc, char *argv[]){ QApplication app(argc, argv); QPixmap pixmap(":/images/move.gif"); QSplashScreen *SplashScreen = new QSplashScreen; SplashScreen->setPixmap(pixmap); SplashScreen->show(); Qt::Alignment align = Qt::AlignTop | Qt::AlignRight; SplashScreen->showMessage("test",align,Qt::red); //设置延时 //第一种方式 QDateTime curDateTime = QDateTime::currentDateTime(); QDateTime nowDateTime; do { nowDateTime = QDateTime::currentDateTime(); app.processEvents(); }while(curDateTime.secsTo(nowDateTime) <= 5); //第二种方式// int nWaitTime = 5000;// QElapsedTimer timer;// timer.start();// while(timer.elapsed() < nWaitTime){// app.processEvents();// } //第三种方式// QDateTime curDateTime = QDateTime::currentDateTime();// QDateTime furDateTime = curDateTime.addSecs(5);// while(curDateTime != furDateTime){// curDateTime = QDateTime::currentDateTime();// app.processEvents();// } MainWindow w; SplashScreen->finish(&w); w.show(); delete SplashScreen; return app.exec();}

 

 参考博客:http://blog.csdn.net/chenlong12580/article/details/23713025

 

转载于:https://www.cnblogs.com/tianzhang/p/4931148.html

你可能感兴趣的文章
Get MAC address using POSIX APIs
查看>>
bzoj2120
查看>>
基于uFUN开发板的心率计(一)DMA方式获取传感器数据
查看>>
【dp】船
查看>>
oracle, group by, having, where
查看>>
⑥python模块初识、pyc和PyCodeObject
查看>>
object-c中管理文件和目录:NSFileManager使用方法
查看>>
Kibana:分析及可视化日志文件
查看>>
nodejs pm2使用
查看>>
cocos2d-x 3.10 PageView BUG
查看>>
装饰器的基本使用:用户登录
查看>>
CSS选择器总结
查看>>
mysql中sql语句
查看>>
head/tail实现
查看>>
sql语句的各种模糊查询语句
查看>>
vlc 学习网
查看>>
Python20-Day05
查看>>
Real World Haskell 第七章 I/O
查看>>
C#操作OFFICE一(EXCEL)
查看>>
【js操作url参数】获取指定url参数值、取指定url参数并转为json对象
查看>>