qt启动等待动态图

cpp 复制代码
#ifndef SPLASHSCREEN_H
#define SPLASHSCREEN_H
#include <QLabel>
#include <QGridLayout>
#include <QTimer>
#include <QPainter>
#include <QMouseEvent>
//启动窗口
class SplashScreen : public QWidget
{
    Q_OBJECT
public:
    explicit SplashScreen(int nLoadingType, QWidget *parent = nullptr);
    ~SplashScreen();
    void setWidget(QWidget *mainWidget);
    QWidget * getWidget();
    void init();
    void  closeWidget();
    void   setLoadText(const QString &title);
    void  setLoadCount(int count);
    void  start();
protected:
    virtual void    mousePressEvent(QMouseEvent *event) override;
    virtual void    mouseMoveEvent(QMouseEvent *event) override;
    virtual void    paintEvent(QPaintEvent *event) override;
signals:
    void    finish();
public slots:
    void   countTimeout();
private:    QGridLayout *   m_pGridLayout;
    QLabel *   m_pNumLabel;  //倒计时计数标签
    QLabel *   m_pTextLabel;  //文字标签
    QWidget *  m_pMainWidget; //保存主窗口
    //SplashScreen显示完毕后,就自动显示m_pMainWidget
    QPoint     m_MousePos;    //保存鼠标位置
    QTimer *   m_pLoadTimer;
    QTimer *   m_pCountTimer;
    int  m_nWidth;    //窗口宽度
    int  m_nHeight;   //窗口高度
    int  m_nAngle;    //旋转角度
    int  m_nCount;    //倒计时描述
    int  m_nLoadingType; //加载样式};

};
#endif // SPLASHSCREEN_H
cpp 复制代码
#include"splashscreen.h"

SplashScreen::SplashScreen(int nLoadingType, QWidget *parent) \
    : QWidget(parent),\
    m_pMainWidget(nullptr)
{
    m_nLoadingType = nLoadingType;
    //加载图形的类型范围为0-8
    if ((m_nLoadingType < 0) ||( m_nLoadingType > 8))
    {
        m_nLoadingType = 0;
    }
    init();
}

SplashScreen::~SplashScreen()
{
    delete m_pLoadTimer;
    delete m_pCountTimer;
}

void SplashScreen::setWidget(QWidget *mainWidget)
    {
         m_pMainWidget = mainWidget;
    }
QWidget *SplashScreen::getWidget()
    {
         return m_pMainWidget;
    }
void SplashScreen::setLoadText(const QString &title)
    {
        if(!title.isEmpty())
        {
            m_pTextLabel->setText(title);
        }
    }

void SplashScreen::setLoadCount(int count)
    {    if(m_nCount != count)
        {
            m_nCount = count;
        }
        m_pNumLabel->setText(QString::number(m_nCount));
    }
void SplashScreen::start()
    {
      //  m_pCountTimer->start();
        m_pLoadTimer->start();
        show();
    }
void SplashScreen::init()
    {
    //setWindowFlags(Qt::Window | Qt::FramelessWindowHint | Qt::WindowStaysOnTopHint);
    //setAttribute(Qt::WA_TranslucentBackground);
    resize(300, 300);
    //setAttribute(Qt::WA_DeleteOnClose);
    m_pNumLabel = new QLabel(this);
    QSizePolicy sizePolicy(QSizePolicy::Preferred, QSizePolicy::Expanding);
    sizePolicy.setHorizontalStretch(0);
    sizePolicy.setVerticalStretch(0);
    sizePolicy.setHeightForWidth(m_pNumLabel->sizePolicy().hasHeightForWidth());
    m_pNumLabel->setSizePolicy(sizePolicy);
    m_pNumLabel->setAlignment(Qt::AlignCenter);
    m_pNumLabel->setStyleSheet(QString::fromUtf8("color: rgb(55, 55, 55);\n"                                                "font: 75 50pt \"\346\245\267\344\275\223\";\n"                                                "background-color: transparent;"));
    m_pTextLabel = new QLabel(this);
    m_pTextLabel->setVisible(false);
    m_pTextLabel->setText(QString("Loading"));
    m_pTextLabel->setMinimumSize(0, 40);
    m_pTextLabel->setMaximumHeight(40);
    m_pTextLabel->setAlignment(Qt::AlignHCenter | Qt::AlignTop);
    m_pTextLabel->setStyleSheet(QString::fromUtf8("color: rgb(55, 55, 55);\n"                                                 "font: 75 14pt \"\346\245\267\344\275\223\";\n"                                                 "background-color: transparent;"));
    m_pGridLayout = new QGridLayout(this);
    m_pGridLayout->setSpacing(0);
    m_pGridLayout->setContentsMargins(11, 11, 11, 11);
    m_pGridLayout->setContentsMargins(70, 70, 70, 70);
    m_pGridLayout->addWidget(m_pNumLabel, 1, 0, 1, 1);
    m_pGridLayout->addWidget(m_pTextLabel, 2, 0, 1, 1);
    setLayout(m_pGridLayout);
    setStyleSheet("QLabel{ color: rgb(55, 55, 55);"
                  "font: 75 50pt \"\346\245\267\344\275\223\";"
                  "background-color: transparent;}");
    m_nWidth = width();
    m_nHeight = height();
    m_pLoadTimer = new QTimer(this);
    connect(m_pLoadTimer, SIGNAL(timeout()), SLOT(update()));
    m_pLoadTimer->setInterval(5);

    m_pCountTimer = new QTimer(this);
    connect(m_pCountTimer, SIGNAL(timeout()), SLOT(countTimeout()));
    m_pCountTimer->setInterval(1000);

    m_nAngle = 0;
    m_nCount = 5;
    m_pNumLabel->setText(QString::number(m_nCount));
 }
void SplashScreen::mousePressEvent(QMouseEvent *event)
 {
    m_MousePos = event->globalPos() - pos();
 }
void SplashScreen::mouseMoveEvent(QMouseEvent *event)
 {
    move(event->globalPos() - m_MousePos);
 }
void SplashScreen::paintEvent(QPaintEvent *event)
 {
    Q_UNUSED(event);
    QPainter painter(this);
    painter.setRenderHint(QPainter::Antialiasing);
    QPixmap pixmap;
    QString strPath = ":/Images/Loading/Loading" + QString::number(m_nLoadingType) + ".png";
    pixmap.load(strPath);
    //平移到中间
    painter.translate(m_nWidth / 2, m_nHeight / 2);
    //旋转角度
    painter.rotate(m_nAngle);

    //图片的旋转轨迹是一个圆圈,必须把图片圆心移到这个轨迹圆,才能拼凑出画面
    painter.translate(-m_nWidth / 2, -m_nHeight / 2);
    //画图片//图片的宽高和窗口宽高是一样的
    painter.drawPixmap(0, 0, m_nWidth, m_nHeight, pixmap);

    //角度每次转2度
    m_nAngle += 2;
    if (m_nAngle >= 360)
    m_nAngle = 0;
 }
void SplashScreen::countTimeout()
 {
    m_nCount--;
     m_pNumLabel->setText(QString::number(m_nCount));
    if (m_nCount <= 0)
     {
         emit finish();
        if(m_pLoadTimer->isActive())
             m_pLoadTimer->stop();
        if(m_pCountTimer->isActive())
            m_pCountTimer->stop();
        close();
        if(m_pMainWidget)
            m_pMainWidget->show();
    }
 }
cpp 复制代码
int nType=0;
    SplashScreen *pSplashScreen=new SplashScreen(nType,nullptr);
    pSplashScreen->setLoadCount(9);
    pSplashScreen->start();