• qt QPainter画灯泡


    在这里插入图片描述

    把画家(坐标原点) 移动 到如图
    然后画家旋转180度
    
    • 1
    • 2

    最重要的是切点坐标

    根据圆心坐标,原点坐标,计算切点坐标
    
    1.用QPainterPath绘制出一个圆
    2.用它减去三角形路径
    3.用画刷刷出上半部分,下半部分
    
    • 1
    • 2
    • 3
    • 4
    • 5

    在这里插入图片描述

    代码太乱,没啥参考性,请自行绘制

    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H
    
    #include 
    
    #include 
    #include 
    #include 
    
    
    QT_BEGIN_NAMESPACE
    namespace Ui { class MainWindow; }
    QT_END_NAMESPACE
    
    class MainWindow : public QMainWindow
    {
        Q_OBJECT
    
    public:
        MainWindow(QWidget *parent = nullptr);
        ~MainWindow();
    
    void paintEvent ( QPaintEvent * event );
    
    QPointF calculateTangentCoordinates(QPointF pointf,double R);
    
    void dreaScanPicture(QPainter &p,int length,int r,QColor color);
    void  setPictureSize(int length,int r,QColor color);
    
    private:
        Ui::MainWindow *ui;
        int length;
        int radius;
        double calculate_angle_arc;
        QColor color;
    
    
    
    };
    #endif // MAINWINDOW_H
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    
    #include 
    #include 
    #include 
    
    MainWindow::MainWindow(QWidget *parent)
        : QMainWindow(parent)
        , ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
    
        QColor color;
        color.setRgb(255,245,0,255);
    
        setPictureSize(200,100,color);
    
    
    }
    
    MainWindow::~MainWindow()
    {
        delete ui;
    }
    
    void MainWindow::setPictureSize(int length, int r, QColor color)
    {
    
        this-> length=length;
        this-> radius=r;
        this-> color=color;
        update();
    
    }
    
    
    
    void MainWindow::paintEvent(QPaintEvent *event)
    {
        QPainter p(this);
    
    
        dreaScanPicture(p,length, radius,color);
    
    
    }
    
    //计算坐标原点到 圆的切线坐标
    QPointF MainWindow::calculateTangentCoordinates(QPointF pointf,double R)
    {
        double y1=pointf.y();
        double r=R;
        double angle;
    
    
        angle=qAsin(r/y1);
    
        double tangentCoordinates_y=qSqrt(y1*y1-r*r)*qCos(angle);
        double tangentCoordinates_x=qSqrt(y1*y1-r*r)*qSin(angle);
    
    
         this->calculate_angle_arc=angle*180/3.14;
        return QPointF(tangentCoordinates_x,tangentCoordinates_y);
    }
    
    
    
    void MainWindow::dreaScanPicture(QPainter &p,int length,int r,QColor color)
    {
        p.setRenderHints(QPainter::Antialiasing | QPainter::TextAntialiasing);
    
         p.translate(this->width()/2,this->height());
    
         p.rotate(180);
    
        int R=r;
        int diameter=2*R;
        int height=length;
    
         QPainterPath circle;
         circle.addEllipse(QPointF(0,height+R),R,R);
    
         QPointF tangent_coordinates= calculateTangentCoordinates(QPointF(0,height+R),R);
         QPainterPath rect;
         rect.addRect(R,tangent_coordinates.y(),-diameter,-diameter);
    
    
         QPainterPath half=circle-rect;
         p.fillPath(half,QBrush(color));
    
         // 设置画刷的透明度
         QBrush brush;
       //  brush.setColor(QColor)  // 设置透明度为128(范围0-255,0为完全透明,255为完全不透明)
    
         p.setBrush(color);
         p.setPen(Qt::NoPen);
    
         QPolygon triangle;
    
         // 计算 过程中
         // 由于画笔宽度 误差,角度的计算精度误差,按照 经验 补偿 为5
         triangle << QPoint(0,0) <
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
  • 相关阅读:
    2022最新发布Java八股文+大厂面试真题, 25 个专题技术点,一线大厂面试题
    因子分析(SPSS和Python)
    PyQt界面里如何加载本地视频以及调用摄像头实时检测(小白入门必看)
    机智云工业级4G Cat.1 DTU全面升级数据采集485/232 GC521
    FFmpeg 6.1 发布,7.0时代即将来临
    计算机毕业设计(附源码)python众筹平台
    OpenHarmony页面级UI状态存储:LocalStorage
    Docker--harbor私有仓库部署与管理
    #力扣:14. 最长公共前缀@FDDLC
    浅谈GPU:历史发展,架构
  • 原文地址:https://blog.csdn.net/sunflower_2020/article/details/136379541