Qt动画,Qt程序开场动画

2023-10-22 06:20
文章标签 程序 qt 动画 开场

本文主要是介绍Qt动画,Qt程序开场动画,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

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

 

#pragma once

#include <QtWidgets/QWidget>
#include "ui_testwelcomeform.h"
#include <QDialog>
#include <QPaintEvent>
#include <QTimer>
#include <QPropertyAnimation>
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 <QPainter>
#include <QDebug>
#include <QDesktopWidget>
#include <QScreen>
#include <QGraphicsDropShadowEffect>
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 <QWidget>
#include "ui_mainform.h"
#include "testwelcomeform.h"
#include <QPropertyAnimation>
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 <QDesktopWidget>
#include <QScreen>
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 <QtWidgets/QApplication>

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

这篇关于Qt动画,Qt程序开场动画的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/259641

相关文章

Qt 中 isHidden 和 isVisible 的区别与使用小结

《Qt中isHidden和isVisible的区别与使用小结》Qt中的isHidden()和isVisible()方法都用于查询组件显示或隐藏状态,然而,它们有很大的区别,了解它们对于正确操... 目录1. 基础概念2. 区别清见3. 实际案例4. 注意事项5. 总结1. 基础概念Qt 中的 isHidd

QT移植到RK3568开发板的方法步骤

《QT移植到RK3568开发板的方法步骤》本文主要介绍了QT移植到RK3568开发板的方法步骤,文中通过图文示例介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一... 目录前言一、获取SDK1. 安装依赖2. 获取SDK资源包3. SDK工程目录介绍4. 获取补丁包二

Qt把文件夹从A移动到B的实现示例

《Qt把文件夹从A移动到B的实现示例》本文主要介绍了Qt把文件夹从A移动到B的实现示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学... 目录如何移动一个文件? 如何移动文件夹(包含里面的全部内容):如何删除文件夹:QT 文件复制,移动(

如何用java对接微信小程序下单后的发货接口

《如何用java对接微信小程序下单后的发货接口》:本文主要介绍在微信小程序后台实现发货通知的步骤,包括获取Access_token、使用RestTemplate调用发货接口、处理AccessTok... 目录配置参数 调用代码获取Access_token调用发货的接口类注意点总结配置参数 首先需要获取Ac

基于Python开发PDF转Doc格式小程序

《基于Python开发PDF转Doc格式小程序》这篇文章主要为大家详细介绍了如何基于Python开发PDF转Doc格式小程序,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 用python实现PDF转Doc格式小程序以下是一个使用Python实现PDF转DOC格式的GUI程序,采用T

Qt实现发送HTTP请求的示例详解

《Qt实现发送HTTP请求的示例详解》这篇文章主要为大家详细介绍了如何通过Qt实现发送HTTP请求,文中的示例代码讲解详细,具有一定的借鉴价值,感兴趣的小伙伴可以跟随小编一起学习一下... 目录1、添加network模块2、包含改头文件3、创建网络访问管理器4、创建接口5、创建网络请求对象6、创建一个回复对

将java程序打包成可执行文件的实现方式

《将java程序打包成可执行文件的实现方式》本文介绍了将Java程序打包成可执行文件的三种方法:手动打包(将编译后的代码及JRE运行环境一起打包),使用第三方打包工具(如Launch4j)和JDK自带... 目录1.问题提出2.如何将Java程序打包成可执行文件2.1将编译后的代码及jre运行环境一起打包2

Qt 中集成mqtt协议的使用方法

《Qt中集成mqtt协议的使用方法》文章介绍了如何在工程中引入qmqtt库,并通过声明一个单例类来暴露订阅到的主题数据,本文通过实例代码给大家介绍的非常详细,感兴趣的朋友一起看看吧... 目录一,引入qmqtt 库二,使用一,引入qmqtt 库我是将整个头文件/源文件都添加到了工程中进行编译,这样 跨平台

在不同系统间迁移Python程序的方法与教程

《在不同系统间迁移Python程序的方法与教程》本文介绍了几种将Windows上编写的Python程序迁移到Linux服务器上的方法,包括使用虚拟环境和依赖冻结、容器化技术(如Docker)、使用An... 目录使用虚拟环境和依赖冻结1. 创建虚拟环境2. 冻结依赖使用容器化技术(如 docker)1. 创

基于Qt Qml实现时间轴组件

《基于QtQml实现时间轴组件》时间轴组件是现代用户界面中常见的元素,用于按时间顺序展示事件,本文主要为大家详细介绍了如何使用Qml实现一个简单的时间轴组件,需要的可以参考下... 目录写在前面效果图组件概述实现细节1. 组件结构2. 属性定义3. 数据模型4. 事件项的添加和排序5. 事件项的渲染如何使用