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

相关文章

【前端学习】AntV G6-08 深入图形与图形分组、自定义节点、节点动画(下)

【课程链接】 AntV G6:深入图形与图形分组、自定义节点、节点动画(下)_哔哩哔哩_bilibili 本章十吾老师讲解了一个复杂的自定义节点中,应该怎样去计算和绘制图形,如何给一个图形制作不间断的动画,以及在鼠标事件之后产生动画。(有点难,需要好好理解) <!DOCTYPE html><html><head><meta charset="UTF-8"><title>06

嵌入式QT开发:构建高效智能的嵌入式系统

摘要: 本文深入探讨了嵌入式 QT 相关的各个方面。从 QT 框架的基础架构和核心概念出发,详细阐述了其在嵌入式环境中的优势与特点。文中分析了嵌入式 QT 的开发环境搭建过程,包括交叉编译工具链的配置等关键步骤。进一步探讨了嵌入式 QT 的界面设计与开发,涵盖了从基本控件的使用到复杂界面布局的构建。同时也深入研究了信号与槽机制在嵌入式系统中的应用,以及嵌入式 QT 与硬件设备的交互,包括输入输出设

JAVA智听未来一站式有声阅读平台听书系统小程序源码

智听未来,一站式有声阅读平台听书系统 🌟&nbsp;开篇:遇见未来,从“智听”开始 在这个快节奏的时代,你是否渴望在忙碌的间隙,找到一片属于自己的宁静角落?是否梦想着能随时随地,沉浸在知识的海洋,或是故事的奇幻世界里?今天,就让我带你一起探索“智听未来”——这一站式有声阅读平台听书系统,它正悄悄改变着我们的阅读方式,让未来触手可及! 📚&nbsp;第一站:海量资源,应有尽有 走进“智听

Flutter 进阶:绘制加载动画

绘制加载动画:由小圆组成的大圆 1. 定义 LoadingScreen 类2. 实现 _LoadingScreenState 类3. 定义 LoadingPainter 类4. 总结 实现加载动画 我们需要定义两个类:LoadingScreen 和 LoadingPainter。LoadingScreen 负责控制动画的状态,而 LoadingPainter 则负责绘制动画。

EMLOG程序单页友链和标签增加美化

单页友联效果图: 标签页面效果图: 源码介绍 EMLOG单页友情链接和TAG标签,友链单页文件代码main{width: 58%;是设置宽度 自己把设置成与您的网站宽度一样,如果自适应就填写100%,TAG文件不用修改 安装方法:把Links.php和tag.php上传到网站根目录即可,访问 域名/Links.php、域名/tag.php 所有模板适用,代码就不粘贴出来,已经打

跨系统环境下LabVIEW程序稳定运行

在LabVIEW开发中,不同电脑的配置和操作系统(如Win11与Win7)可能对程序的稳定运行产生影响。为了确保程序在不同平台上都能正常且稳定运行,需要从兼容性、驱动、以及性能优化等多个方面入手。本文将详细介绍如何在不同系统环境下,使LabVIEW开发的程序保持稳定运行的有效策略。 LabVIEW版本兼容性 LabVIEW各版本对不同操作系统的支持存在差异。因此,在开发程序时,尽量使用

CSP 2023 提高级第一轮 CSP-S 2023初试题 完善程序第二题解析 未完

一、题目阅读 (最大值之和)给定整数序列 a0,⋯,an−1,求该序列所有非空连续子序列的最大值之和。上述参数满足 1≤n≤105 和 1≤ai≤108。 一个序列的非空连续子序列可以用两个下标 ll 和 rr(其中0≤l≤r<n0≤l≤r<n)表示,对应的序列为 al,al+1,⋯,ar​。两个非空连续子序列不同,当且仅当下标不同。 例如,当原序列为 [1,2,1,2] 时,要计算子序列 [

这些心智程序你安装了吗?

原文题目:《为什么聪明人也会做蠢事(四)》 心智程序 大脑有两个特征导致人类不够理性,一个是处理信息方面的缺陷,一个是心智程序出了问题。前者可以称为“认知吝啬鬼”,前几篇文章已经讨论了。本期主要讲心智程序这个方面。 心智程序这一概念由哈佛大学认知科学家大卫•帕金斯提出,指个体可以从记忆中提取出的规则、知识、程序和策略,以辅助我们决策判断和解决问题。如果把人脑比喻成计算机,那心智程序就是人脑的

用Unity2D制作一个人物,实现移动、跳起、人物静止和动起来时的动画:中(人物移动、跳起、静止动作)

上回我们学到创建一个地形和一个人物,今天我们实现一下人物实现移动和跳起,依次点击,我们准备创建一个C#文件 创建好我们点击进去,就会跳转到我们的Vision Studio,然后输入这些代码 using UnityEngine;public class Move : MonoBehaviour // 定义一个名为Move的类,继承自MonoBehaviour{private Rigidbo

uniapp设置微信小程序的交互反馈

链接:uni.showToast(OBJECT) | uni-app官网 (dcloud.net.cn) 设置操作成功的弹窗: title是我们弹窗提示的文字 showToast是我们在加载的时候进入就会弹出的提示。 2.设置失败的提示窗口和标签 icon:'error'是设置我们失败的logo 设置的文字上限是7个文字,如果需要设置的提示文字过长就需要设置icon并给