Qt用c++/python 分别绘制传输带

#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
cpp 复制代码
#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实现代码

python 复制代码
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())
相关推荐
蜀黍@猿17 分钟前
C/C++基础错题归纳
c++
Kai HVZ22 分钟前
python爬虫----爬取视频实战
爬虫·python·音视频
古希腊掌管学习的神25 分钟前
[LeetCode-Python版]相向双指针——611. 有效三角形的个数
开发语言·python·leetcode
m0_7482448328 分钟前
StarRocks 排查单副本表
大数据·数据库·python
雨中rain32 分钟前
Linux -- 从抢票逻辑理解线程互斥
linux·运维·c++
B站计算机毕业设计超人34 分钟前
计算机毕业设计PySpark+Hadoop中国城市交通分析与预测 Python交通预测 Python交通可视化 客流量预测 交通大数据 机器学习 深度学习
大数据·人工智能·爬虫·python·机器学习·课程设计·数据可视化
路人甲ing..37 分钟前
jupyter切换内核方法配置问题总结
chrome·python·jupyter
游客5201 小时前
opencv中的常用的100个API
图像处理·人工智能·python·opencv·计算机视觉
每天都要学信号1 小时前
Python(第一天)
开发语言·python
凡人的AI工具箱1 小时前
每天40分玩转Django:Django国际化
数据库·人工智能·后端·python·django·sqlite