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

相关文章

Django调用外部Python程序的完整项目实战

《Django调用外部Python程序的完整项目实战》Django是一个强大的PythonWeb框架,它的设计理念简洁优雅,:本文主要介绍Django调用外部Python程序的完整项目实战,文中通... 目录一、为什么 Django 需要调用外部 python 程序二、三种常见的调用方式方式 1:直接 im

Qt实现对Word网页的读取功能

《Qt实现对Word网页的读取功能》文章介绍了几种在Qt中实现Word文档(.docx/.doc)读写功能的方法,包括基于QAxObject的COM接口调用、DOCX模板替换及跨平台解决方案,重点讨论... 目录1. 核心实现方式2. 基于QAxObject的COM接口调用(Windows专用)2.1 环境

Mysql 驱动程序的程序小结

《Mysql驱动程序的程序小结》MySQL驱动程序是连接应用程序与MySQL数据库的重要组件,根据不同的编程语言和应用场景,MySQL提供了多种驱动程序,下面就来详细的了解一下驱动程序,感兴趣的可以... 目录一、mysql 驱动程序的概念二、常见的 MySQL 驱动程序1. MySQL Connector

Qt实现删除布局与布局切换功能

《Qt实现删除布局与布局切换功能》在Qt应用开发中,动态管理布局是一个常见需求,比如根据用户操作动态删除某个布局,或在不同布局间进行切换,本文将详细介绍如何实现这些功能,并通过完整示例展示具体操作,需... 目录一、Qt动态删除布局1. 布局删除的注意事项2. 动态删除布局的实现步骤示例:删除vboxLay

java程序远程debug原理与配置全过程

《java程序远程debug原理与配置全过程》文章介绍了Java远程调试的JPDA体系,包含JVMTI监控JVM、JDWP传输调试命令、JDI提供调试接口,通过-Xdebug、-Xrunjdwp参数配... 目录背景组成模块间联系IBM对三个模块的详细介绍编程使用总结背景日常工作中,每个程序员都会遇到bu

uni-app小程序项目中实现前端图片压缩实现方式(附详细代码)

《uni-app小程序项目中实现前端图片压缩实现方式(附详细代码)》在uni-app开发中,文件上传和图片处理是很常见的需求,但也经常会遇到各种问题,下面:本文主要介绍uni-app小程序项目中实... 目录方式一:使用<canvas>实现图片压缩(推荐,兼容性好)示例代码(小程序平台):方式二:使用uni

python获取指定名字的程序的文件路径的两种方法

《python获取指定名字的程序的文件路径的两种方法》本文主要介绍了python获取指定名字的程序的文件路径的两种方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要... 最近在做项目,需要用到给定一个程序名字就可以自动获取到这个程序在Windows系统下的绝对路径,以下

QT Creator配置Kit的实现示例

《QTCreator配置Kit的实现示例》本文主要介绍了使用Qt5.12.12与VS2022时,因MSVC编译器版本不匹配及WindowsSDK缺失导致配置错误的问题解决,感兴趣的可以了解一下... 目录0、背景:qt5.12.12+vs2022一、症状:二、原因:(可以跳过,直奔后面的解决方法)三、解决方

基于Python编写自动化邮件发送程序(进阶版)

《基于Python编写自动化邮件发送程序(进阶版)》在数字化时代,自动化邮件发送功能已成为企业和个人提升工作效率的重要工具,本文将使用Python编写一个简单的自动化邮件发送程序,希望对大家有所帮助... 目录理解SMTP协议基础配置开发环境构建邮件发送函数核心逻辑实现完整发送流程添加附件支持功能实现htm

C#控制台程序同步调用WebApi实现方式

《C#控制台程序同步调用WebApi实现方式》控制台程序作为Job时,需同步调用WebApi以确保获取返回结果后执行后续操作,否则会引发TaskCanceledException异常,同步处理可避免异... 目录同步调用WebApi方法Cls001类里面的写法总结控制台程序一般当作Job使用,有时候需要控制