PyQt5-绘制各种图形

27 篇文章 1 订阅

PyQt5-绘制各种图形

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2021/6/21 01:30
# @Author : kevin
# @Site :
# @File : 绘制各种图形.py
# @Software: PyCharm

"""
绘制各种图形

弧
圆形
矩形(正方形)
多边形
虚线画图形
渐变色图形
渐变色字体
绘制图像

"""
import sys, math
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *
import sys
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtCore import Qt
from PyQt5.QtGui import (QPen, QPainter, QBrush, QLinearGradient, QConicalGradient, QRadialGradient)
from PyQt5.QtWidgets import (QApplication, QWidget, QGridLayout, QFrame, QSizePolicy)


class DrawAll(QWidget):
    def __init__(self):
        super(DrawAll, self).__init__()
        self.resize(900, 600)
        self.setWindowTitle('绘制各种图形—By:小丑不冷')

    def paintEvent(self, event):
        qp = QPainter()
        pen = QPen()
        qp.begin(self)

        #  todo 画网格代码可不要开始(方便理解)-----------------------------------------------
        x, y, w, h, number = 0, 0, 900, 0, 6  # x:起点横坐标,y:起点纵坐标,w:长度,h:高
        qp.setPen(QPen(Qt.black, 2, Qt.DashLine))  # 设置画笔颜色,画笔粗细,画笔的画线类型(虚线)
        qp.drawLine(150, 0, 150, 600)  # 画第一条竖直虚线
        qp.drawLine(300, 0, 300, 600)  # 画第二条竖直虚线
        qp.drawLine(450, 0, 450, 600)  # 画第三条竖直虚线
        qp.drawLine(600, 0, 600, 600)  # 画第四条竖直虚线
        qp.drawLine(900, 0, 900, 600)  # 画第五条竖直虚线,竖直边界
        while number > 1:
            # 画横虚线
            qp.drawLine(x, y, w, h)
            w = 600
            y += 150
            h += 150
            number -= 1
            if number == 2:  # 画最后一条横线时,需要加长到900
                w += 300

        # todo 画网格代码不要结束-------------------------------------------------------------------

        pen.setWidth(4)  # 设置画笔粗细
        pen.setColor(Qt.blue)  # 设置画笔颜色:蓝
        qp.setPen(pen)  # 设置的画笔
        # 绘制弧
        rect = QRect(30, 25, 100, 100)  # (x:起始横坐标,y:起始纵坐标,w:长,h:高)
        #  alen: 1个 alen 等于1/16度,例如90°则需要 90 * 16
        qp.drawArc(rect, 90 * 16, 90 * 16)  # 90 * 16 旋转角度, 90 * 16 弧度

        # 绘制圆
        pen.setColor(Qt.red)  # 设置画笔颜色:红
        qp.setPen(pen)  # 设置的画笔
        qp.drawArc(180, 25, 100, 100, 0 * 16, 360 * 16)

        # 绘制椭圆
        pen.setColor(Qt.yellow)  # 设置画笔颜色:黄
        qp.setPen(pen)  # 设置的画笔
        qp.drawArc(10, 200, 130, 50, 0 * 16, 360 * 16)

        # 绘制带弦的弧
        pen.setColor(Qt.green)  # 设置画笔颜色:绿
        qp.setPen(pen)  # 设置的画笔
        qp.drawChord(180, 200, 100, 50, 0 * 16, 180 * 16)  # 横起,纵起,弦长,弧高,旋转角度,结束弧度

        # 绘制扇形
        pen.setColor(Qt.gray)  # 设置画笔颜色:灰
        qp.setPen(pen)  # 设置的画笔
        qp.drawPie(0, 350, 150, 150, 45 * 16, 90 * 16)  # 横起,纵起,弦长,弧高,旋转角度,

        # 多边形
        pen.setColor(Qt.magenta)  # 设置画笔颜色:洋红色
        qp.setPen(pen)  # 设置的画笔
        """
        坐标需要精确计算,以point1为初始点
        point2坐标连接point1,point3坐标连接point2
        以此类推,最后point6连接point1
        """
        point1 = QPoint(200, 325)  # 坐标点一
        point2 = QPoint(260, 325)  # 坐标点二
        point3 = QPoint(285, 375)  # 坐标点三
        point4 = QPoint(260, 425)  # 坐标点四
        point5 = QPoint(200, 425)  # 坐标点五
        point6 = QPoint(175, 375)  # 坐标点六
        polygon = QPolygon([point1, point2, point3, point4, point5, point6])  # 六个点坐标
        qp.drawPolygon(polygon)  # 画多边形

        # 绘制多个虚线圆
        pen = QColor(0, 0, 0)  # 设置画笔初始颜色
        pen.setNamedColor('#7300FF')  # 设置色号
        qp.setPen(QPen(pen, 3, Qt.DashLine))  # 设置画笔颜色, 粗细, 画线方式
        qp.drawArc(20, 480, 100, 100, 0, 360 * 16)  # 坐标
        while True:
            for Arc_x in range(2, 5):
                qp.drawArc(Arc_x * 17, Arc_x * 10 + 480, Arc_x * 10, Arc_x * 10, 0, 360 * 16)  # 坐标
            break

        # 绘制点线正方形
        pen = QColor(0, 0, 0)  # 设置画笔初始颜色
        pen.setNamedColor('#ff8b02')  # 设置色号
        qp.setPen(QPen(pen, 3, Qt.DashDotLine))  # 设置画笔颜色, 粗细, 画线方式
        point1 = QPoint(170, 490)  # 点坐标一
        point2 = QPoint(280, 490)  # 点坐标二
        point3 = QPoint(280, 560)  # 点坐标三
        point4 = QPoint(170, 560)  # 点坐标四
        polygon = QPolygon([point1, point2, point3, point4])  # 四个点坐标
        qp.drawPolygon(polygon)  # 画多边形的方式画矩形

        # 绘制渐变色线段
        pen = QLinearGradient(QPointF(300, 10), QPointF(300, 100))  # 设置颜色起始,结束位置
        pen.setColorAt(0, Qt.magenta)  # 设置渐变颜色一
        pen.setColorAt(1, Qt.green)  # 设置渐变颜色二
        qp.setPen(QPen(pen, 5, Qt.SolidLine))  # 画实线段
        qp.drawLine(335, 20, 335, 130)  # 实线段坐标
        qp.setPen(QPen(pen, 5, Qt.DashLine))  # 画虚线段
        qp.drawLine(355, 20, 355, 140)  # 虚线段坐标
        qp.setPen(QPen(pen, 5, Qt.DashDotLine))  # 画点划线段
        qp.drawLine(375, 20, 375, 140)  # 点划线段坐标
        qp.setPen(QPen(pen, 5, Qt.DotLine))  # 画密集虚线段
        qp.drawLine(395, 20, 395, 140)  # 密集虚线段坐标
        qp.setPen(QPen(pen, 5, Qt.DashDotDotLine))  # 点点划线段
        qp.drawLine(415, 20, 415, 140)  # 点点划线段坐标

        # 渐变色字体(色彩区域暂时未能调试成功)
        pen = QLinearGradient(QPointF(350, 10), QPointF(350, 100))  # 设置颜色起始,结束位置
        pen.setColorAt(0, Qt.red)  # 设置渐变颜色一
        pen.setColorAt(1, Qt.blue)  # 设置渐变颜色二
        qp.setFont(QFont('SimSun', 20))  # 设置字体,字号
        qp.setBrush(pen)  # 画刷
        qp.drawText(480, 70, "测试字体")  # 文本位置,文本内容

        # 绘制图像
        image = QImage(r'C:\Users\点雨洛\Desktop\小人.png')  # 图片路径
        rect = QRect(600, 0, image.width()/3.2, image.height()/3.95)  # 图片放置坐标,长,高缩放的倍数
        qp.drawImage(rect, image)  # 画出图片

        qp.end()  # 结束整个画图

#  todo 后续补充中...


if __name__ == '__main__':
    app = QApplication(sys.argv)
    main = DrawAll()
    main.show()
    sys.exit(app.exec_())

在这里插入图片描述

  • 3
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
PyQt5 中,可以通过重新实现图元的 `shape()` 函数来增大线段图元的选中范围。`shape()` 函数返回一个 `QPainterPath` 对象,用于定义图元的形状。 下面是一个示例代码,演示如何增大线段图元的选中范围: ```python from PyQt5.QtCore import Qt, QRectF from PyQt5.QtGui import QPainter, QPainterPath from PyQt5.QtWidgets import QApplication, QGraphicsItem, QGraphicsScene, QGraphicsView class CustomGraphicsItem(QGraphicsItem): def __init__(self): super().__init__() def boundingRect(self): return QRectF(-10, -10, 20, 20) # 设置图元的范围 def shape(self): path = QPainterPath() path.addRect(-20, -2, 40, 4) # 增大线段图元的形状范围 return path def paint(self, painter, option, widget): painter.setPen(Qt.red) painter.drawLine(-20, 0, 20, 0) if __name__ == '__main__': app = QApplication([]) scene = QGraphicsScene() view = QGraphicsView(scene) view.show() item = CustomGraphicsItem() scene.addItem(item) app.exec_() ``` 在上述代码中,我们自定义了一个 `CustomGraphicsItem` 类,重写了其中的 `boundingRect()`、`shape()` 和 `paint()` 方法。在 `boundingRect()` 方法中,我们设置了图元的范围,这是用于限定图元的绘制和碰撞检测的区域;在 `shape()` 方法中,我们创建了一个矩形的 `QPainterPath` 对象,来定义线段图元的形状范围。通过调整 `addRect()` 方法的参数,可以控制线段图元形状的大小和位置。 你可以根据自己的需求调整代码中的参数,实现对线段图元选中范围的增大。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值