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

网站兼容性代码北京通州区网站建设

网站兼容性代码,北京通州区网站建设,电子商务网站建设设计报告,南宁seo外包服务商《Qt动画编程实战:轻松实现头像旋转效果》 Qt 提供了丰富的动画框架,可以轻松实现各种平滑的动画效果。其中,旋转动画是一种常见的 UI 交互方式,广泛应用于加载指示器、按钮动画、场景变换等。本篇文章将详细介绍如何使用 Qt 实现…

《Qt动画编程实战:轻松实现头像旋转效果》

Qt 提供了丰富的动画框架,可以轻松实现各种平滑的动画效果。其中,旋转动画是一种常见的 UI 交互方式,广泛应用于加载指示器、按钮动画、场景变换等。本篇文章将详细介绍如何使用 Qt 实现旋转动画。
在这里插入图片描述

1、效果

在这里插入图片描述

2、具体实现

#ifndef ROTATINGIMAGE_H
#define ROTATINGIMAGE_H#include <QWidget>
#include <QLabel>
#include <QPropertyAnimation>class RotatingImage : public QWidget
{Q_OBJECTQ_PROPERTY(qreal rotation READ rotation WRITE setRotation)public:explicit RotatingImage(QWidget *parent = nullptr);qreal rotation() const { return m_rotation; }void setRotation(qreal rotation);public slots:void startRotation();void stopRotation();void pauseRotation();void resumeRotation();void setRotationDuration(int msecs);protected:void resizeEvent(QResizeEvent *event) override;private:void updatePixmap();QPixmap getScaledPixmap() const;private:QLabel *imageLabel;QPropertyAnimation *rotationAnimation;qreal m_rotation;QPixmap originalPixmap;QSize targetSize;
};#endif // ROTATINGIMAGE_H #include "rotatingimage.h"
#include <QPixmap>
#include <QTransform>
#include <QVBoxLayout>
#include <QResizeEvent>
#include <QPainter>
#include <QEasingCurve>RotatingImage::RotatingImage(QWidget *parent): QWidget(parent), m_rotation(0)
{// 创建布局QVBoxLayout *layout = new QVBoxLayout(this);layout->setContentsMargins(0, 0, 0, 0);// 创建标签并设置图片imageLabel = new QLabel(this);imageLabel->setFixedSize(QSize(200, 200));originalPixmap.load(":/images/test.png");// 设置目标大小targetSize = QSize(200, 200);  // 比Label小一点,留出边距// 初始化图片updatePixmap();imageLabel->setAlignment(Qt::AlignCenter);imageLabel->setStyleSheet("QLabel { border-radius: 100px; background: transparent; }");layout->addWidget(imageLabel, 0, Qt::AlignCenter);// 设置动画rotationAnimation = new QPropertyAnimation(this, "rotation", this);rotationAnimation->setStartValue(0.0);rotationAnimation->setEndValue(360.0);rotationAnimation->setDuration(5000);rotationAnimation->setLoopCount(-1);// 使用QEasingCurve使动画更流畅rotationAnimation->setEasingCurve(QEasingCurve::Linear);rotationAnimation->start();
}void RotatingImage::setRotation(qreal rotation)
{if (m_rotation != rotation) {m_rotation = rotation;updatePixmap();}
}void RotatingImage::updatePixmap()
{QPixmap scaledPix = getScaledPixmap();// 创建一个透明的目标图片,大小与Label相同QPixmap targetPixmap(imageLabel->size());targetPixmap.fill(Qt::transparent);// 在目标图片上绘制旋转后的图片QPainter painter(&targetPixmap);painter.setRenderHint(QPainter::Antialiasing);painter.setRenderHint(QPainter::SmoothPixmapTransform);// 计算中心点QPointF center = targetPixmap.rect().center();painter.translate(center);painter.rotate(m_rotation);painter.translate(-center);// 计算绘制位置使图片居中QPointF drawPos((targetPixmap.width() - scaledPix.width()) / 2.0,(targetPixmap.height() - scaledPix.height()) / 2.0);painter.drawPixmap(drawPos, scaledPix);painter.end();imageLabel->setPixmap(targetPixmap);
}QPixmap RotatingImage::getScaledPixmap() const
{return originalPixmap.scaled(targetSize,Qt::KeepAspectRatio,Qt::SmoothTransformation);
}void RotatingImage::resizeEvent(QResizeEvent *event)
{QWidget::resizeEvent(event);updatePixmap();
}void RotatingImage::startRotation()
{rotationAnimation->start();
}void RotatingImage::stopRotation()
{rotationAnimation->stop();
}void RotatingImage::pauseRotation()
{rotationAnimation->pause();
}void RotatingImage::resumeRotation()
{rotationAnimation->resume();
}void RotatingImage::setRotationDuration(int msecs)
{rotationAnimation->setDuration(msecs);
} #include <QApplication>
#include "rotatingimage.h"int main(int argc, char *argv[])
{QApplication app(argc, argv);RotatingImage *rotatingImage = new RotatingImage();rotatingImage->resize(400, 400);rotatingImage->show();return app.exec();
} 

3| 结语

Qt 的动画系统提供了丰富的 API,可以方便地实现旋转动画。本文介绍了 QPropertyAnimation 的基础用法、QWidgetQPainter 旋转方法,以及更高级的优化方案。希望这些内容能帮助你在实际开发中更好地使用 Qt 动画!
源码地址:https://github.com/MingYueRuYa/QtDemo

http://www.yayakq.cn/news/738377/

相关文章:

  • 如何管理网站域名域名做网站名
  • 网站优化软件排名器简单的网站设计模板下载
  • 网站编程代码如何做哟个优惠券网站
  • 怎么做盗版网站吗.net 响应式网站
  • 网站推广策划思路的内容wordpress直接
  • 网站的内容规划怎么写花钱做网站需要所有权
  • 网站域名备案与解析阿里 wordpress
  • 网站 目录访问想要给网站加视频怎么做
  • 珠海品牌机械网站建设网络推广合作
  • 要制作自己的网站需要什么千图网app下载
  • 施工员证查询官方网站seo网站优化推广怎么做
  • 实体店营销策划公司cn域名做seo
  • 企业网站托管常见问题网站建设 管理规范
  • 个人做多个网站备案怎么做流量网站
  • 生活服务网站建设方案百度网站登录入口
  • 网站建设的方法有四种旅游类网站开发毕业设计
  • 青岛三吉互联网站建设公司昆明猫咪科技网站建设公司
  • 代码做网站图片怎么插优酷视频接到网站怎么做
  • 网站开发时遇不到算法河南专业的做网站的公司
  • 局域网手机网站建设品牌设计公司排行榜前十名
  • 注册网站要百度实名认证安不安全手机网站管理
  • wordpress站点用户注册办公设备网站推广怎么做
  • 个人网站设计成品下载做网站需要多少钱 网络服务
  • 做律师网站推广优化哪家好app外包公司怎么找
  • 2013一汽大众企业网站车主俱乐部建设维护方案海宁市住房与建设规划局网站
  • 外贸网站建设内容包括哪些南庄顺德网站建设
  • c 网站开发用的人多吗南昌网站忧化
  • 开发青年网站深圳网站建设哪里
  • 湘潭什么网站做c1题目wordpress对接公众号
  • 广东品牌网站建设哪家好哈尔滨道外区建设局官方网站