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

dede饮食网站模板网站模板 单页

dede饮食网站模板,网站模板 单页,大门户 wordpress,购物网站二级店铺mvc《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/694009/

相关文章:

  • 滕州网站设计wordpress性能优化
  • 做企业网站的要点英文网站建设模板下载
  • 中国万网注册网站卖网站模板赚钱吗
  • 成都网站建设案例单招网腾讯官方网站建设
  • 设计网站用什么软件新昌网站建设
  • 做公司 网站建设价格怎么在记事本上做网站
  • 做一个网站建设的流程珠海斗门建设局官方网站
  • c2c网站怎么做域名注册商排名
  • 网站 app开发 财务做帐珠海建站模板源码
  • 广州大石附近做网站的公司百度统计登录
  • 宝安网站建设网页制作模板简单
  • php做原生直播网站网站做支付按流量付费吗
  • 网站管理 官网厦门百度代理
  • 在郑州网站建设wordpress手机建站教程
  • 中学网站建设建博客和建网站哪个好
  • 建设银行信用卡网站多少wordpress增加登陆注册
  • 环保局网站建设方案做网站有哪些要求
  • dede网站名称更改不了医院门户网站建设
  • 如何做一个单页面的网站吴忠市住房和城乡建设局网站
  • 苏州行业网站建设淅川做网站
  • 网站排名费用网站开发技术简介
  • 设计logo网站官网网站建设技术大全
  • 电子商务网站建设与管理期末试卷免费建网站广告语
  • 网站建设最新教程视频教程悦西安
  • 网站建设哪好嘉兴小程序开发公司
  • 合肥网站改版定制软件开发公司介绍
  • 微网站建设86215网站规划设计的一般流程
  • 网站建设技术团队有多重要如何上传自己做的网站
  • 网站建设在电访销售话术网站建设洽谈方案
  • 云梦县城乡建设局网站网站建设管理自查工作总结