HMI-2-[QSplashScreen]:启动动画

基于Qt的汽车仪表模拟

特别鸣谢,Qt QSplashScreen显示动画(gif效果)和消息_lxj434368832的博客-CSDN博客_qsplashscreen

开始的想法很简单,在第一个版本中那个启动画面,那是一个静止的启动画面,在这个版本中,打算做一个可以动的,带有进度条的动画版的启动画面。本来想整个widget了,奈何自己道行太浅,实现不了,就只好作罢,在尝试带有进度条的版本的时候,发现用传统的动画方式,会将进度条遮住,使用静态图片就没有问题,如下图所示,但是弄成这样,就不是我想要的东西,所以就有了下面第二张图的样子了。

在第二张图中,进度条不是使用代码生成的,是在制作GIF图时就压制进去的。

这个实现,主要参考了文档头链接中的内容,甚至有的内容只是直接复制过来的;哈哈哈。

这里我把这个东西分装成了一个类“Loader”,这类其实是重在了QSplashScreen类。头文件如下

#ifndef LOADER_H
#define LOADER_H
#include <QObject>
#include <QMovie>
#include <QSplashScreen>
class Loader : public QSplashScreen
{

    Q_OBJECT

public:
    Loader(const QPixmap & pixmap);
    Loader(const QString gifname);
    ~Loader();
protected:
    virtual void drawContents(QPainter *painter);
    void setGif(QString fileName);
private slots:
    void slot_update();

private:
    QMovie *m_movie = NULL;
    int m_framecount;
};

#endif // LOADER_H

源文件如下

#include "loader.h"
#include <QPixmap>
#include <QPainter>
#include <QLabel>
#include <QTimer>
#include <QDebug>
Loader::Loader(const QPixmap &pixmap):QSplashScreen(pixmap)
{

}
Loader::Loader(const QString gifname)
{
    setGif(gifname);
    QPixmap pixmap(gifname);
    this->setPixmap(pixmap);
}
Loader::~Loader()
{

}
void Loader::setGif(QString fileName)
{
    m_movie = new QMovie(fileName);
    m_framecount = m_movie->frameCount();
    connect(m_movie,&QMovie::frameChanged,this,[=](int temp){
        if(temp == (m_framecount-1))
        {
            m_movie->stop();
        }
    });
    m_movie->start();
    QTimer *timer = new QTimer(this);
    connect(timer,SIGNAL(timeout()),this,SLOT(slot_update()));
    timer->start(40);
}
void Loader::drawContents(QPainter *painter)
{
    painter->setFont(QFont("黑体", 10));
    painter->setPen(QColor(213, 218, 220));
    painter->drawText(QPointF(5, 15), "Version: 1.0.0");
    QSplashScreen::drawContents(painter);
}

void Loader::slot_update()
{
    this->setPixmap(m_movie->currentPixmap());
    repaint();
}

调用方式

int main(int argc,char *argv[])
{
    QApplication a(argc,argv);
    Loader *loader = new Loader(":/Core/Resources/Core/LoaderImage.gif");
    loader->show();
    QDateTime n=QDateTime::currentDateTime();
    QDateTime now;
    do
    {
        now=QDateTime::currentDateTime();
        a.processEvents();
    }while (n.secsTo(now)<=4);
    ControlPanel controlPanel;
    controlPanel.show();
    loader->finish(&controlPanel);
    delete loader;
    return a.exec();
}

附件:另一种实现方式:

#include "mainwindow.h"
#include <QApplication>
#include <QSplashScreen>
#include <QPixmap>
#include <QLabel>
#include <QMovie>
#include <QThread>
int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    QPixmap pixmap("./images/login.gif");
    QSplashScreen splash(pixmap);
    QMovie *movie = new QMovie("./images/login.gif");
    QLabel *label = new QLabel(&splash);
    label->setMovie(movie);
    movie->start();
    splash.show();
    for(int i = 0;i<11000;i+=movie->speed())
    {
        QCoreApplication::processEvents();
        QThread::usleep(500*movie->speed());
    }
    MainWindow w;
    w.show();
    splash.finish(&w);
    return a.exec();
}

2019/08/12 23:55

说明:

本项目中所使借鉴原型来自:[吉利] 博瑞GE | 仪表HMI设计吉利汽车HMI项目

设计图的所有权和解释权都归吉利汽车所有。

本项目所有资源文件均有打不死的小海复刻制作。

本项目代码暂时不会开源,有需要的源码的可与我联系,左上角二维码加微信。

本项目仅限学习交流、禁止商业使用。

  • 4
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 6
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

DreamLife.

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值