效果:

代码:

#ifndef PLAYSTATECONTROL_H
#define PLAYSTATECONTROL_H#include <QWidget>
#include <QTimer>class PlayStateControl : public QWidget
{Q_OBJECTpublic:PlayStateControl(QWidget *parent = nullptr);~PlayStateControl()override;protected:void paintEvent(QPaintEvent *event)override;void mousePressEvent(QMouseEvent *event)override;private:QPixmap diskPixmap;QPixmap polePixmap;QPixmap centerPixmap;bool isPlaying{true};qreal angle{0};QTimer angleTimer;qreal poleSpinAngle{0};bool poleIsSpin{false};QTimer poleSpinTimer;
};
#endif // PLAYSTATECONTROL_H
#include "playstatecontrol.h"
#include <QPainter>
#include <QPaintEvent>
#include <QPainterPath>
#include <QDebug>PlayStateControl::PlayStateControl(QWidget *parent): QWidget(parent)
{setPalette(Qt::white);diskPixmap.load(":/mediaDisk.png");polePixmap.load(":/Pole.png");centerPixmap.load(":/jay.jpg");setMinimumSize(300,250);connect(&angleTimer,&QTimer::timeout,[this]{angle += 1.0;if(angle == 360.0)angle = 0.0;update();});angleTimer.setInterval(40);connect(&poleSpinTimer,&QTimer::timeout,[this]{if(isPlaying){poleSpinAngle -= 1.0;if(poleSpinAngle == 0.0){poleSpinTimer.stop();}}else{poleSpinAngle += 1.0;if(poleSpinAngle > 25.0){poleSpinTimer.stop();}}update();});poleSpinTimer.setInterval(20);
}PlayStateControl::~PlayStateControl()
{
}void PlayStateControl::paintEvent(QPaintEvent *event)
{QPainter painter(this);painter.setRenderHint(QPainter::Antialiasing,true);painter.setRenderHint(QPainter::SmoothPixmapTransform,true);const auto rect = event->rect();auto radius = std::min(rect.width(),rect.height()) / 2 - 25;QRect rectangle(-radius,-radius,radius * 2,radius * 2);painter.translate(rect.center());painter.setPen(Qt::transparent);painter.drawPixmap(rectangle,diskPixmap);auto centerImgRadius = radius / 1.6;auto centerImgRect = QRect(-centerImgRadius, -centerImgRadius, centerImgRadius*2, centerImgRadius*2);painter.save();QRegion maskedRegion(centerImgRect, QRegion::Ellipse);painter.setClipRegion(maskedRegion);painter.rotate(angle);painter.drawPixmap(centerImgRect,centerPixmap);painter.restore();auto poleRect = QRect(rectangle.topRight().x(),rectangle.topRight().y(),rect.width()*0.15,rect.height()*0.5);auto polePixmapTemp = polePixmap.scaled(poleRect.size(),Qt::KeepAspectRatio);auto difference = polePixmapTemp.width() + radius - (rect.width()/2);if(difference > 0){poleRect.setX(poleRect.x() - difference);}painter.save();painter.translate(poleRect.topLeft());painter.rotate(poleSpinAngle);painter.drawPixmap(QPoint(0,0),polePixmapTemp);painter.restore();
}void PlayStateControl::mousePressEvent(QMouseEvent *event)
{isPlaying = !isPlaying;poleIsSpin = true;poleSpinTimer.start();if(isPlaying){angleTimer.stop();}else{angleTimer.start();}QWidget::mousePressEvent(event);
}

用到的图片:

Qt编写自定义控件:唱片播放控件相关推荐

  1. Qt编写自定义控件插件路过的坑及注意事项

    在一日一控件的口号下,终于写好了五十几个自定义控件,包括各种仪表盘,各种温度计,各种进度条,各种按钮等,具体可参见(http://www.cnblogs.com/feiyangqingyun/p/61 ...

  2. Qt界面开发(各种控件以及图表)

    Qt界面开发(各种控件以及图表) 1.Qt简洁窗体 源代码链接:点击打开链接 2.QT漂亮界面 源代码链接:点击打开链接 3.音乐播放器界面 源代码链接:点击打开链接 4.六宫格界面 源代码链接:点击 ...

  3. java 用户控件_C#自定义控件VS用户控件

    C#中自定义控件VS用户控件大比拼 1 自定义控件与用户控件区别 WinForm中, 用户控件(User Control):继承自 UserControl,主要用于开发 Container 控件,Co ...

  4. Qt界面源码 -- 各种控件以及图表

    原文链接:https://blog.csdn.net/zhangxiaoyu_sy/article/details/78925221#comments 资源已经放到 链接:https://pan.ba ...

  5. C#自定义控件VS用户控件

    C#自定义控件VS用户控件 1.C#中自定义控件VS用户控件大比拼 2.为自定义控件(或类)的方法属性添加注解 2.1.Description:在属性窗口中添加属性及属性说明 2.2.Browsabl ...

  6. 基于GDI+用C#编写的.NET流程图控件开发周记(2011-08-05)

    花了差不多一个月的业余时间,新编写了一个流程图控件(用于.NET和C#),这个控件现在终于有了一个原型.控件可以用在主界面的导航画面,也可以代替Visio来绘制流程图,最重要的是可以用于日后的工作流功 ...

  7. Qt QWidget实现消息提示控件TipsWidget

    前言 用Qt实现一个消息提示控件,控件宽度会根据显示的内容多少来动态伸展,控件显示三秒钟过后会自动渐变透明度然后最终消失,这有点类似于Android的Toast控件,都是用于消息短暂提示. 源码 头文 ...

  8. uniapp 判断页面是否是横竖屏,解决微信小程序video组件全屏播放视频遮盖自定义播放控件问题

    如果res.deviceOrientation 等于landscape 的话是竖屏,portrait则是横屏.因为用户每旋转一次屏幕就会触发里面的onShow钩子,因此在页面显示或横竖屏变化都会触发这 ...

  9. Flash播放控件属性详解

    Flash 播放控件属性详解 一.属性篇 1.AlignMode(读写)  语法:AlignMode As Long  说明:对齐方式(与SAlign 属性联动).当控件的长宽比例与影片不一致且WMo ...

最新文章

  1. 【NOIP2007】【Luogu1094】纪念品分组(贪心,乘船问题)
  2. 类库探源——System.Exception
  3. Win10 IoT 10 中文显示乱码或报错的问题
  4. list Control实现单元格编辑与插入Combo Box
  5. 《vue+vant 文本超出两行部分省略号显示》
  6. java 修改txt_Java实现批量修改txt文件名称的方法示例
  7. python跨域攻击教学_关于python 跨域处理方式详解
  8. SQL Server中的bcp实用工具(批量复制程序)简介
  9. TIOBE 1 月编程语言排行榜:C 语言再度「C 位」出道!
  10. 畅销书系列:《引爆点》、《异类》、《从0到1》
  11. Java编程风格与命名规范整理(转载)
  12. 提升企业网站流量转化率的核心是什么
  13. 地温梯度 河南_河南省地热(温泉)分布规律
  14. Android中设置位移动画的停止
  15. 定积分计算(谭浩强c语言第5版p272)
  16. Godaddy Backorder 失败后申请退款。
  17. java回复功能_Java实现评论回复功能
  18. SQL注入-显错注入
  19. 电脑录音,教你电脑怎么录音
  20. ubuntu14.04下安装图片编辑软件pinta

热门文章

  1. 强化学习算法A2C(Advantage Actor-Critic)和A3C(Asynchronous Advantage Actor-Critic)算法详解以及A2C的Pytorch实现
  2. [译] 为什么设计师讨厌政治(如何解决)
  3. Android各个版本的区别
  4. 如何成为一名优秀的后端工程师
  5. C++语法整理(From GitHub)
  6. 开启和关闭远程Windows系统3389端口
  7. 儿童学编程的原因以及方法
  8. CMake设置Visual Studio工程的调试环境变量和工作目录cwd的方法
  9. Android NDK jni开发,适当的给Android 代码加密
  10. 初识C51单片机(详解)