#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include<QPainter>
#include<QPropertyAnimation>
#include<QPen>
class Widget : public QWidget
{
Q_OBJECT
public:
Widget(QWidget *parent = nullptr);
~Widget();
private:
void paintEvent(QPaintEvent *event) override;
int angel=0;
QPropertyAnimation *ani;
};
#endif // WIDGET_H
#include "widget.h"
#include<QDebug>
#include<QVector>
Widget::Widget(QWidget *parent)
: QWidget(parent)
{
this->resize(400,100);
//创建属性动画
ani = new QPropertyAnimation(this);
ani->setTargetObject(this);
ani->setStartValue(0);
ani->setEndValue(260);
ani->setDuration(500);
ani->setLoopCount(-1);
ani->start();
connect(ani,&QPropertyAnimation::valueChanged,this,[=](const QVariant& value){
angel=value.toInt();
// qDebug()<<value;
update();
});
}
Widget::~Widget()
{
}
void Widget::paintEvent(QPaintEvent *event){
Q_UNUSED(event);
QPainter painter(this);
painter.setRenderHint(QPainter::Antialiasing,true);
QRect rect_left = QRect(10,10,height()-20,height()-20);
QRect rect_right = QRect(width()-height(),10,height()-20,height()-20);
painter.setBrush(Qt::NoBrush);
painter.setPen(QPen(Qt::black,3));
painter.drawEllipse(rect_left);
painter.drawEllipse(rect_right);
//绘制直线
QPainterPath path ;
//圆上面的线
painter.save();
QPen pen;
pen.setStyle(Qt::CustomDashLine);
pen.setWidth(3);
QVector<qreal> list{6,4,6,4};
pen.setDashPattern(list);
pen.setDashOffset(angel);
painter.setPen(pen);
path.moveTo(rect_left.center().x(),rect_left.center().y()-rect_left.width()/2);
path.lineTo(rect_right.center().x(),rect_right.center().y()-rect_right.width()/2);
path.moveTo(rect_right.center().x(),rect_right.center().y()+rect_right.width()/2);
path.lineTo(rect_left.center().x(),rect_left.center().y()+rect_left.width()/2);
painter.drawPath(path);
painter.restore();
painter.save();
painter.translate(rect_left.center());
painter.rotate(angel);
int temp = rect_left.width()/2;
painter.setPen(Qt::NoPen);
painter.setBrush(Qt::blue);
painter.drawEllipse(-10,-10,20,20);
painter.drawPie(-temp+10,-temp+10,2*(temp-10),2*(temp-10),0*16,60*16);
painter.drawPie(-temp+10,-temp+10,2*(temp-10),2*(temp-10),120*16,60*16);
painter.drawPie(-temp+10,-temp+10,2*(temp-10),2*(temp-10),240*16,60*16);
painter.restore();
//右边圆
painter.save();
painter.translate(rect_right.center());
painter.rotate(angel);
int temp2 = rect_left.width()/2;
painter.setPen(Qt::NoPen);
painter.setBrush(Qt::blue);
painter.drawEllipse(-10,-10,20,20);
painter.drawPie(-temp2+10,-temp2+10,2*(temp2-10),2*(temp2-10),0*16,60*16);
painter.drawPie(-temp2+10,-temp2+10,2*(temp2-10),2*(temp2-10),120*16,60*16);
painter.drawPie(-temp2+10,-temp2+10,2*(temp2-10),2*(temp2-10),240*16,60*16);
painter.restore();
}
效果:
pyhton实现代码
import sys
from PySide6.QtWidgets import *
from PySide6.QtCore import *
from PySide6.QtGui import *
class Belt(QWidget):
def __init__(self,parent=None,*args,**kwargs):
super().__init__(parent,*args,**kwargs)
#属性动画
self.ani=QPropertyAnimation(self)
self.ani.setTargetObject(self)
self.ani.setStartValue(0)
self.ani.setEndValue(220)
self.ani.setDuration(400)
self.ani.setLoopCount(-1)
self.ani.valueChanged.connect(self._on_ani)
self._angle = 0
self.ani.start()
def _on_ani(self,value:int):
self._angle =int(value)
self.update()
def paintEvent(self, event):
painter = QPainter(self)
painter.setRenderHint(QPainter.RenderHint.Antialiasing,True)
left_rect = QRect(10,10,self.height()-20,self.height()-20)
right_rect = QRect(self.width()-self.height(),10,self.height()-20,self.height()-20)
#绘制左右圆
painter.setPen(QPen(Qt.black,3))
painter.setBrush(Qt.BrushStyle.NoBrush)
painter.drawEllipse(left_rect)
painter.drawEllipse(right_rect)
path = QPainterPath()
pen= QPen()
painter.save()
# painter.translate(left_rect.center())
pen.setStyle(Qt.PenStyle.CustomDashLine)
pen.setDashPattern([5,3,5,3])
pen.setDashOffset(-self._angle)
pen.setWidth(3)
painter.setPen(pen)
path.moveTo(left_rect.center().x(),left_rect.center().y()-left_rect.height()/2)
path.lineTo(right_rect.center().x(),right_rect.center().y()-right_rect.height()/2)
path.moveTo(right_rect.center().x(), right_rect.center().y() + right_rect.height() / 2)
path.lineTo(left_rect.center().x(), left_rect.center().y() + left_rect.height() / 2)
painter.drawPath(path)
painter.restore()
#绘制左扇形
painter.save()
pen.setStyle(Qt.PenStyle.SolidLine)
painter.setBrush(Qt.blue)
painter.translate(left_rect.center())
painter.rotate(self._angle)
r=left_rect.width()-10
half_r = int(r/2)
painter.drawPie(-half_r,-half_r,r,r,0*16,60*16)
painter.drawPie(-half_r,-half_r,r,r,120*16,60*16)
painter.drawPie(-half_r,-half_r,r,r,240*16,60*16)
painter.drawEllipse(-10, -10, 20, 20)
painter.restore()
# 绘制右扇形
painter.save()
pen.setStyle(Qt.PenStyle.SolidLine)
painter.setBrush(Qt.blue)
painter.translate(right_rect.center())
painter.rotate(self._angle)
r = right_rect.width() - 10
half_r = int(r / 2)
painter.drawPie(-half_r, -half_r, r, r, 0 * 16, 60 * 16)
painter.drawPie(-half_r, -half_r, r, r, 120 * 16, 60 * 16)
painter.drawPie(-half_r, -half_r, r, r, 240 * 16, 60 * 16)
painter.drawEllipse(-10, -10, 20, 20)
painter.restore()
if __name__ == '__main__':
app = QApplication(sys.argv)
win = Belt()
win.show()
sys.exit(app.exec())