#include "RoundImageWidget.h"
#include <QTimer>
#include <QPainter>
#include <QDebug>
RoundImageWidget::RoundImageWidget(QWidget *parent) : QWidget(parent)
{
m_rotateAngle = 0.0;
QString backgroundImageName(":/Images/thumb.jpg");
bool isLoadOk = m_backgroundImage.load(backgroundImageName);
if (!isLoadOk) {
qDebug()<< "load" << backgroundImageName << "failed !";
}
}
void RoundImageWidget::startRotateAnimation()
{
if (nullptr == m_timer) {
// 通过定时器去旋转图片,可根据自身需求去设定旋转速度
int speed = 150;
qreal angle = 1.0;
m_timer = new QTimer(this);
m_timer->setInterval(speed);
connect(m_timer, &QTimer::timeout, [=]() {
m_rotateAngle += angle;
if (m_rotateAngle > 360.0) {
m_rotateAngle = angle;
}
repaint();
});
}
m_timer->start();
}
void RoundImageWidget::paintEvent(QPaintEvent *event)
{
Q_UNUSED(event);
if(!m_backgroundImage.isNull()){
// 中心点
int centerPosW = width() >> 1;
int centerPosH = height() >> 1;
QPainter painter(this);
painter.setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform);
// 把自身截取为圆形
QPainterPath path;
int round = qMin(width(), height());
path.addEllipse(0, 0, round, round);
painter.setClipPath(path);
// 设置中心点为起点
painter.translate(centerPosW, centerPosH);
// 开始旋转
painter.rotate(m_rotateAngle);
// 恢复起点位置,即左上角(0,0)
painter.translate(-centerPosW, -centerPosH);
// 绘制图片
painter.drawPixmap(-1, -1, width()+2, height()+2, m_backgroundImage);
}
}
qt生成圆形图片并实现自动旋转,类似音乐播放器中旋转专辑封面
4星 · 超过85%的资源 需积分: 35 28 浏览量
2018-07-03
17:39:51
上传
评论 2
收藏 89KB ZIP 举报
__不白也不黑
- 粉丝: 3
- 资源: 2
最新资源
- PHP端通过modbus协议跟第三方设备进行数据通信
- navicat安装包亲测可用
- 算法部署-使用OpenVINO部署MobileStyleGAN轻量化高保真图像合成算法-项目源码-优质项目实战.zip
- 基于java实现远程采集华为逆变器使用modbus tcp协议进行通讯的设备数据
- Unity画面共享Spout插件
- 基于C++用modbus实现的工业设备的数据采集程序,支持Tcp、串口
- 完结12章AI Agent智能应用从0到1定制开发
- 15白落梅:你是锦瑟我为流年:三毛的万水千山-3491776.mobi
- Federated Learning-Aided Prognostics in the Shipping 4.0: Princi
- OFDM 的鲁棒频率和定时同步文献部分阅读笔记
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈