Qt动画,Qt程序开场动画

Qt动画,Qt程序开场动画_第1张图片

设计思路:qt动画 让欢迎界面显示完全,然后缩放欢迎界面,缩放到一定层度就关闭界面,然后显示主界面并放大

 

#pragma once

#include
#include "ui_testwelcomeform.h"
#include
#include
#include
#include
class TestWelcomeForm : public QDialog
{
    Q_OBJECT

public:
    TestWelcomeForm(QWidget *parent = Q_NULLPTR);
    ~TestWelcomeForm();
protected:
    void paintEvent(QPaintEvent *event);
private:
    void init();
private slots:
    void slotActiveLabel2();//让第二个label动起来
    void slotActiveLabel3();
    void slotActiveLabel4();
    void slotActiveWidget();//让主题动起来
    void slotWidgetShrink();//窗口缩小
    void slotActiveTimer();//启动定时器
    void slotCloseForm();//关闭界面

private:
    Ui::TestWelcomeFormClass ui;
    QPoint                    m_pos1;
    QPoint                    m_pos2;
    QPoint                    m_pos3;
    QPoint                    m_pos4;
    QPoint                    m_pos5;
    QPropertyAnimation*        m_animationLabel1 = nullptr;
    QPropertyAnimation*        m_animationLabel2 = nullptr;
    QPropertyAnimation*        m_animationLabel3 = nullptr;
    QPropertyAnimation*        m_animationLabel4 = nullptr;
    QPropertyAnimation*        m_animationWidget = nullptr;
    QPropertyAnimation*        m_animationWidgetShrink = nullptr;
    QTimer*                    m_timer = nullptr;
};

#include "testwelcomeform.h"
#include
#include
#include
#include
#include
TestWelcomeForm::TestWelcomeForm(QWidget *parent)
    : QDialog(parent)
{
    ui.setupUi(this);

    init();
}

TestWelcomeForm::~TestWelcomeForm()
{
    if (m_animationLabel1 != nullptr)
    {
        delete m_animationLabel1;
        m_animationLabel1 = nullptr;
    }
    if (m_animationLabel2 != nullptr)
    {
        delete m_animationLabel2;
        m_animationLabel2 = nullptr;
    }
    if (m_animationLabel3 != nullptr)
    {
        delete m_animationLabel3;
        m_animationLabel3 = nullptr;
    }
    if (m_animationLabel4 != nullptr)
    {
        delete m_animationLabel4;
        m_animationLabel4 = nullptr;
    }
    if (m_animationWidget = nullptr)
    {
        delete m_animationWidget;
        m_animationWidget = nullptr;
    }
    if (m_animationWidgetShrink = nullptr)
    {
        delete m_animationWidgetShrink;
        m_animationWidgetShrink = nullptr;
    }
    if (m_timer = nullptr)
    {
        delete m_timer;
        m_timer = nullptr;
    }
        
}

void TestWelcomeForm::paintEvent(QPaintEvent *event)
{
    QPainter painter(this);
    painter.fillRect(this->rect(),QColor(0,0,0));
    painter.drawPixmap(this->rect(), QPixmap(":/img/bkg.jpg"));

}

void TestWelcomeForm::init()
{
    this->setWindowFlag(Qt::FramelessWindowHint);
    QGraphicsDropShadowEffect *shadow = new QGraphicsDropShadowEffect(this);
    shadow->setOffset(0, 0);
    shadow->setColor(QColor("#444444"));
    shadow->setBlurRadius(15);
    ui.frame->setGraphicsEffect(shadow);
    QRect desktopRect_1 = QApplication::desktop()->availableGeometry();
    QScreen* currentScreen = QApplication::screenAt(QCursor::pos());
    // 获取当前活动屏幕的几何信息
    QRect desktopRect_2 = currentScreen->availableGeometry();
    if (desktopRect_1 == desktopRect_2) {
        this->setGeometry(QApplication::desktop()->availableGeometry().width() / 2 - this->width() / 2, QApplication::desktop()->availableGeometry().height() / 2 - this->height() / 2, this->width(), this->height());
    }
    else {
        this->setGeometry(QApplication::desktop()->availableGeometry().width() / 2 - this->width() / 2 + desktopRect_1.width(), QApplication::desktop()->availableGeometry().height() / 2 - this->height() / 2, this->width(), this->height());
    }

    ui.widget->setVisible(false);
    ui.label_2->setVisible(false);
    ui.label_3->setVisible(false);
    ui.label_4->setVisible(false);
    m_pos1 = ui.label_1->geometry().topLeft();
    m_pos2 = ui.label_2->geometry().topLeft();
    m_pos3 = ui.label_3->geometry().topLeft();
    m_pos4 = ui.label_4->geometry().topLeft();
    m_pos5 = ui.widget->geometry().topLeft();
    ui.label_1->move(QPoint(m_pos1.x(), 0));
    ui.label_2->move(QPoint(m_pos2.x(), 0));
    ui.label_3->move(QPoint(m_pos3.x(), 0));
    ui.label_4->move(QPoint(m_pos4.x(), 0));
    ui.widget->move(QPoint(m_pos5.x(), 0));

    m_animationLabel1 = new QPropertyAnimation(ui.label_1, "pos");
    m_animationLabel1->setEasingCurve(QEasingCurve::OutElastic);
    m_animationLabel1->setDuration(200);
    m_animationLabel1->setStartValue(QPoint(m_pos1.x(), 0));
    m_animationLabel1->setEndValue(m_pos1);

    m_animationLabel2 = new QPropertyAnimation(ui.label_2, "pos");
    m_animationLabel2->setEasingCurve(QEasingCurve::OutElastic);
    m_animationLabel2->setDuration(500);
    m_animationLabel2->setStartValue(QPoint(m_pos2.x(), 0));
    m_animationLabel2->setEndValue(m_pos2);

    m_animationLabel3 = new QPropertyAnimation(ui.label_3, "pos");
    m_animationLabel3->setEasingCurve(QEasingCurve::OutElastic);
    m_animationLabel3->setDuration(200);
    m_animationLabel3->setStartValue(QPoint(m_pos3.x(), 0));
    m_animationLabel3->setEndValue(m_pos3);

    m_animationLabel4 = new QPropertyAnimation(ui.label_4, "pos");
    m_animationLabel4->setEasingCurve(QEasingCurve::OutElastic);
    m_animationLabel4->setDuration(500);
    m_animationLabel4->setStartValue(QPoint(m_pos4.x(), 0));
    m_animationLabel4->setEndValue(m_pos4);

    m_animationWidget = new QPropertyAnimation(ui.widget, "pos");
    m_animationWidget->setEasingCurve(QEasingCurve::OutBounce);
    m_animationWidget->setDuration(500);
    m_animationWidget->setStartValue(QPoint(m_pos5.x(), 0));
    m_animationWidget->setEndValue(m_pos5);

    m_animationWidgetShrink = new QPropertyAnimation(this, "geometry");
    m_animationWidgetShrink->setStartValue(this->geometry());
    m_animationWidgetShrink->setDuration(800);

    if (desktopRect_1 == desktopRect_2)
    {
        m_animationWidgetShrink->setEndValue(QRect(QApplication::desktop()->availableGeometry().width() / 2, QApplication::desktop()->availableGeometry().height() / 2, 30, 30));
    }
    else
    {
        m_animationWidgetShrink->setEndValue(QRect(QApplication::desktop()->availableGeometry().width() / 2 + desktopRect_2.width(), QApplication::desktop()->availableGeometry().height() / 2, 30, 30));
    }
    
    connect(m_animationLabel1, SIGNAL(finished()), this, SLOT(slotActiveLabel2()));
    connect(m_animationLabel2, SIGNAL(finished()), this, SLOT(slotActiveLabel3()));
    connect(m_animationLabel3, SIGNAL(finished()), this, SLOT(slotActiveLabel4()));
    connect(m_animationLabel4, SIGNAL(finished()), this, SLOT(slotActiveWidget()));
    connect(m_animationWidget, SIGNAL(finished()), this, SLOT(slotActiveTimer()));
    connect(m_animationWidgetShrink, SIGNAL(finished()), this, SLOT(slotCloseForm()));
    m_animationLabel1->start();
}

void TestWelcomeForm::slotActiveLabel2()
{
    ui.label_2->setVisible(true);
    m_animationLabel2->start();
}

void TestWelcomeForm::slotActiveLabel3()
{
    ui.label_3->setVisible(true);
    m_animationLabel3->start();
}

void TestWelcomeForm::slotActiveLabel4()
{
    ui.label_4->setVisible(true);
    m_animationLabel4->start();
}

void TestWelcomeForm::slotActiveWidget()
{
    ui.widget->setVisible(true);
    m_animationWidget->start();
}

void TestWelcomeForm::slotWidgetShrink()
{
    m_timer->stop();
    m_animationWidgetShrink->start();
}

void TestWelcomeForm::slotActiveTimer()
{
    m_timer = new QTimer(this);
    connect(m_timer, SIGNAL(timeout()), this, SLOT(slotWidgetShrink()));
    m_timer->start(500);
}

void TestWelcomeForm::slotCloseForm()
{
    this->close();
}

#pragma once

#include
#include "ui_mainform.h"
#include "testwelcomeform.h"
#include
class mainForm : public QWidget
{
    Q_OBJECT

public:
    mainForm(QWidget *parent = Q_NULLPTR);
    ~mainForm();
public slots:
    void slotShowControl();
private:
    void hidenControl();
private:
    Ui::mainForm ui;
    QPropertyAnimation*        m_animation = nullptr;
};
 

#include "mainform.h"
#include
#include
mainForm::mainForm(QWidget *parent)
    : QWidget(parent)
{
    ui.setupUi(this);
    hidenControl();
    QRect desktopRect_1 = QApplication::desktop()->availableGeometry();
    QScreen* currentScreen = QApplication::screenAt(QCursor::pos());
    // 获取当前活动屏幕的几何信息

    QRect desktopRect_2 = currentScreen->availableGeometry();
    if (desktopRect_1 == desktopRect_2) {
        this->setGeometry(QRect(QApplication::desktop()->availableGeometry().width() / 2, QApplication::desktop()->availableGeometry().height() / 2, 10, 10));
    }
    else {
        this->setGeometry(QRect(QApplication::desktop()->availableGeometry().width() / 2 + desktopRect_2.width(), QApplication::desktop()->availableGeometry().height() / 2, 10, 10));
    }
    m_animation = new QPropertyAnimation(this, "geometry");
    m_animation->setDuration(500);
    m_animation->setStartValue(this->geometry());
    if (desktopRect_1 == desktopRect_2){
        m_animation->setEndValue(QRect(QApplication::desktop()->availableGeometry().width() / 4, QApplication::desktop()->availableGeometry().height() / 4, QApplication::desktop()->availableGeometry().width() / 2, QApplication::desktop()->availableGeometry().height() / 2));
    }else{
        m_animation->setEndValue(QRect(QApplication::desktop()->availableGeometry().width() / 4 + desktopRect_2.width(), QApplication::desktop()->availableGeometry().height() / 4, QApplication::desktop()->availableGeometry().width() / 2, QApplication::desktop()->availableGeometry().height() / 2));
    }
    m_animation->start();
    connect(m_animation, SIGNAL(finished()), this, SLOT(slotShowControl()));
}

mainForm::~mainForm()
{
}

void mainForm::slotShowControl()
{
    ui.label->setVisible(true);
    this->setWindowState(Qt::WindowMaximized);
}

void mainForm::hidenControl()
{
    ui.label->setVisible(false);
    
}
 

#include "testwelcomeform.h"
#include "mainform.h"
#include

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    TestWelcomeForm w;
    w.exec();
    mainForm w2;
    w2.show();
    return a.exec();
}

你可能感兴趣的:(燃犀的QT笔记,qt)