其他分享
首页 > 其他分享> > PyQt5基础学习-QPainter(绘制不同样式的直线) 1. QPen(设置颜色,设置大小, 设置样式) 2.painter.drawLine(绘制直线的范围) 3.pen.setStyle(Q

PyQt5基础学习-QPainter(绘制不同样式的直线) 1. QPen(设置颜色,设置大小, 设置样式) 2.painter.drawLine(绘制直线的范围) 3.pen.setStyle(Q

作者:互联网

绘制实线,虚线,虚点线, 点线, 点点样式和自定义样式

"""
绘制不同类型的直线
"""
"""
用像素点绘制正弦曲线

-2PI  2PI
drawPoint(x, y)

"""
import sys, math
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import Qt

class DrawMultiLine(QWidget):
    def __init__(self):
        super(DrawMultiLine, self).__init__()
        self.resize(300, 300)
        self.setWindowTitle('设置Pen的样式')

    def paintEvent(self, event):
        painter = QPainter(self)
        painter.begin(self)

        #设置画笔的颜色, 字体大小, 线的实心样式
        pen = QPen(Qt.red, 3, Qt.SolidLine)
        #设置画笔
        painter.setPen(pen)
        #绘制线
        painter.drawLine(20, 40, 250, 40)

        #设置画笔的虚线样式
        pen.setStyle(Qt.DashLine)
        painter.setPen(pen)
        painter.drawLine(20, 80, 250, 80)

        #设置画笔的虚线和点样式
        pen.setStyle(Qt.DashDotLine)
        painter.setPen(pen)
        painter.drawLine(20, 120, 250, 120)

        #设置画笔的点线样式
        pen.setStyle(Qt.DotLine)
        painter.setPen(pen)
        painter.drawLine(20, 160, 250, 160)

        #设置虚线的点点样式
        pen.setStyle(Qt.DashDotDotLine)
        painter.setPen(pen)
        painter.drawLine(20, 200, 250, 200)

        #设置自定义线
        pen.setStyle(Qt.CustomDashLine)
        pen.setDashPattern([1, 10, 5, 4])
        painter.setPen(pen)
        painter.drawLine(20, 240, 250, 240)

        painter.end()


if __name__ == "__main__":
    app = QApplication(sys.argv)

    main = DrawMultiLine()
    main.show()

    sys.exit(app.exec_())

 

标签:__,Qt,DashDotLine,self,drawLine,pen,虚线,painter
来源: https://www.cnblogs.com/hyq-lst/p/15858563.html