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())
相关推荐
ULTRA??3 分钟前
C加加中的结构化绑定(解包,折叠展开)
开发语言·c++
测试杂货铺7 分钟前
外包干了2年,快要废了。。
自动化测试·软件测试·python·功能测试·测试工具·面试·职场和发展
艾派森11 分钟前
大数据分析案例-基于随机森林算法的智能手机价格预测模型
人工智能·python·随机森林·机器学习·数据挖掘
小码的头发丝、37 分钟前
Django中ListView 和 DetailView类的区别
数据库·python·django
凌云行者39 分钟前
OpenGL入门005——使用Shader类管理着色器
c++·cmake·opengl
凌云行者42 分钟前
OpenGL入门006——着色器在纹理混合中的应用
c++·cmake·opengl
spygg1 小时前
Qt低版本多网卡组播bug
qt·组播·多网卡组播·qt5.7.0
~yY…s<#>1 小时前
【刷题17】最小栈、栈的压入弹出、逆波兰表达式
c语言·数据结构·c++·算法·leetcode
Chef_Chen1 小时前
从0开始机器学习--Day17--神经网络反向传播作业
python·神经网络·机器学习
码农客栈2 小时前
qt QWebSocketServer详解
qt