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

相关文章

golang程序打包成脚本部署到Linux系统方式

《golang程序打包成脚本部署到Linux系统方式》Golang程序通过本地编译(设置GOOS为linux生成无后缀二进制文件),上传至Linux服务器后赋权执行,使用nohup命令实现后台运行,完... 目录本地编译golang程序上传Golang二进制文件到linux服务器总结本地编译Golang程序

Qt使用QSqlDatabase连接MySQL实现增删改查功能

《Qt使用QSqlDatabase连接MySQL实现增删改查功能》这篇文章主要为大家详细介绍了Qt如何使用QSqlDatabase连接MySQL实现增删改查功能,文中的示例代码讲解详细,感兴趣的小伙伴... 目录一、创建数据表二、连接mysql数据库三、封装成一个完整的轻量级 ORM 风格类3.1 表结构

使用Docker构建Python Flask程序的详细教程

《使用Docker构建PythonFlask程序的详细教程》在当今的软件开发领域,容器化技术正变得越来越流行,而Docker无疑是其中的佼佼者,本文我们就来聊聊如何使用Docker构建一个简单的Py... 目录引言一、准备工作二、创建 Flask 应用程序三、创建 dockerfile四、构建 Docker

Qt QCustomPlot库简介(最新推荐)

《QtQCustomPlot库简介(最新推荐)》QCustomPlot是一款基于Qt的高性能C++绘图库,专为二维数据可视化设计,它具有轻量级、实时处理百万级数据和多图层支持等特点,适用于科学计算、... 目录核心特性概览核心组件解析1.绘图核心 (QCustomPlot类)2.数据容器 (QCPDataC

Qt如何实现文本编辑器光标高亮技术

《Qt如何实现文本编辑器光标高亮技术》这篇文章主要为大家详细介绍了Qt如何实现文本编辑器光标高亮技术,文中的示例代码讲解详细,具有一定的借鉴价值,有需要的小伙伴可以了解下... 目录实现代码函数作用概述代码详解 + 注释使用 QTextEdit 的高亮技术(重点)总结用到的关键技术点应用场景举例示例优化建议

Qt 设置软件版本信息的实现

《Qt设置软件版本信息的实现》本文介绍了Qt项目中设置版本信息的三种常用方法,包括.pro文件和version.rc配置、CMakeLists.txt与version.h.in结合,具有一定的参考... 目录在运行程序期间设置版本信息可以参考VS在 QT 中设置软件版本信息的几种方法方法一:通过 .pro

python编写朋克风格的天气查询程序

《python编写朋克风格的天气查询程序》这篇文章主要为大家详细介绍了一个基于Python的桌面应用程序,使用了tkinter库来创建图形用户界面并通过requests库调用Open-MeteoAPI... 目录工具介绍工具使用说明python脚本内容如何运行脚本工具介绍这个天气查询工具是一个基于 Pyt

Ubuntu设置程序开机自启动的操作步骤

《Ubuntu设置程序开机自启动的操作步骤》在部署程序到边缘端时,我们总希望可以通电即启动我们写好的程序,本篇博客用以记录如何在ubuntu开机执行某条命令或者某个可执行程序,需要的朋友可以参考下... 目录1、概述2、图形界面设置3、设置为Systemd服务1、概述测试环境:Ubuntu22.04 带图

Python程序打包exe,单文件和多文件方式

《Python程序打包exe,单文件和多文件方式》:本文主要介绍Python程序打包exe,单文件和多文件方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录python 脚本打成exe文件安装Pyinstaller准备一个ico图标打包方式一(适用于文件较少的程

VS配置好Qt环境之后但无法打开ui界面的问题解决

《VS配置好Qt环境之后但无法打开ui界面的问题解决》本文主要介绍了VS配置好Qt环境之后但无法打开ui界面的问题解决,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要... 目UKeLvb录找到Qt安装目录中designer.UKeLvBexe的路径找到vs中的解决方案资源