当前位置: 首页 > backend >正文

QT动画类

Qt 中,动画是通过 Qt Animation Framework 实现的,它提供了强大且灵活的工具来创建平滑的动画效果。以下是一些关键点和实现方式:


1. 使用 QPropertyAnimation

QPropertyAnimation 是最常用的类,用于对对象的属性进行动画处理。

示例:移动一个按钮

复制代码#include <QApplication>
#include <QPushButton>
#include <QPropertyAnimation>int main(int argc, char *argv[]) {QApplication app(argc, argv);QPushButton button("点击我");button.resize(100, 50);button.show();QPropertyAnimation animation(&button, "geometry");animation.setDuration(1000); // 动画持续时间:1秒animation.setStartValue(QRect(0, 0, 100, 50)); // 起始位置animation.setEndValue(QRect(200, 200, 100, 50)); // 结束位置animation.start();return app.exec();
}

2. 使用 QSequentialAnimationGroup 和 QParallelAnimationGroup

这两个类可以组合多个动画,分别实现顺序并行播放。

示例:顺序动画

复制代码#include <QApplication>
#include <QPushButton>
#include <QPropertyAnimation>
#include <QSequentialAnimationGroup>int main(int argc, char *argv[]) {QApplication app(argc, argv);QPushButton button("动画按钮");button.resize(100, 50);button.show();QPropertyAnimation *moveAnimation = new QPropertyAnimation(&button, "geometry");moveAnimation->setDuration(1000);moveAnimation->setStartValue(QRect(0, 0, 100, 50));moveAnimation->setEndValue(QRect(200, 200, 100, 50));QPropertyAnimation *resizeAnimation = new QPropertyAnimation(&button, "geometry");resizeAnimation->setDuration(1000);resizeAnimation->setStartValue(QRect(200, 200, 100, 50));resizeAnimation->setEndValue(QRect(200, 200, 200, 100));QSequentialAnimationGroup group;group.addAnimation(moveAnimation);group.addAnimation(resizeAnimation);group.start();return app.exec();
}

3. 使用 QGraphicsView 动画

如果你使用 QGraphicsView 框架,可以通过 QGraphicsItemAnimationQGraphicsObject 实现动画。

示例:旋转一个图形对象

复制代码#include <QApplication>
#include <QGraphicsView>
#include <QGraphicsRectItem>
#include <QPropertyAnimation>int main(int argc, char *argv[]) {QApplication app(argc, argv);QGraphicsScene scene;QGraphicsRectItem *rect = scene.addRect(0, 0, 100, 100);rect->setBrush(Qt::blue);QGraphicsView view(&scene);view.show();QPropertyAnimation animation(rect, "rotation");animation.setDuration(2000);animation.setStartValue(0);animation.setEndValue(360);animation.start();return app.exec();
}

4. 动画的高级功能

  • 缓动曲线 (Easing Curves):通过 QEasingCurve 设置动画的加速、减速效果。
  • 时间线 (QTimeLine):用于更复杂的时间控制。
  • 自定义动画:继承 QAbstractAnimation 实现自定义逻辑。

http://www.xdnf.cn/news/10699.html

相关文章:

  • 使用 Haproxy 搭建高可用 Web 群集
  • 守护进程导致程序kill掉后被重新拉起
  • Java集合初始化:Lists.newArrayList vs new ArrayList()
  • 线程安全 — 场景、解决、悲观锁、乐观锁
  • mysql离线安装教程
  • 计算机视觉NeRF
  • 【GESP真题解析】第 6 集 GESP 三级 2023 年 9 月编程题 1:小杨的储蓄
  • 电路图识图基础知识-高、低压供配电系统电气系统的继电自动装置(十三)
  • android binder(三)binder.c函数分析
  • 审计- 1- 审计概述
  • Python-matplotlib中的Pyplot API和面向对象 API
  • UE5 创建2D角色帧动画学习笔记
  • 网络节点排查
  • RAG系统中如何检测幻觉?
  • 【dshow】VIDEOINFOHEADER2 头文件
  • Arch安装megaton
  • PHP7+MySQL5.6 查立得轻量级公交查询系统
  • ck-editor5的研究 (5):优化-页面离开时提醒保存,顺便了解一下 Editor的生命周期 和 6大编辑器类型
  • 【LeetCode 题解】两数之和(C++/Python 双解法):从语法到算法的全面解析
  • #14 学习日志
  • ②Pybullet干涉检查指令getContactPoints与 getClosestPoints介绍
  • Vue-5-基于JavaScript和plotly.js绘制数据分析类图表
  • ubuntu22.04安装megaton
  • 图像任务中的并发处理:线程池、Ray、Celery 和 asyncio 的比较
  • 经典数学教材推荐(AI相关)
  • rabbitmq Fanout交换机简介
  • 二叉查找树 —— 最近公共祖先问题解析(Leetcode 235)
  • 什么是绿电直连
  • ESP32之Linux编译环境搭建流程
  • 电脑wifi显示已禁用怎么点都无法启用