PyQt5最全27 绘图之drawLine绘制不同类型的直线
import sys
from PyQt5.QtWidgets import QApplication, QWidget
from PyQt5.QtGui import QPainter, QPen
from PyQt5.QtCore import Qt
class DrawMultiLine(QWidget):
"""
drawLine绘制不同类型的直线
QPainter
painter = QPainter()
painter.begin()
painter.drawText(...)
painter.end()
必须在事件方法中绘制各种元素
"""
def __init__(self):
super(DrawMultiLine, self).__init__()
self.setWindowTitle('设置Pen的样式')
self.resize(300, 200)
def paintEvent(self, event):
painter = QPainter()
painter.begin(self)
pen = QPen(Qt.red, 2, Qt.SolidLine)
painter.setPen(pen)
painter.drawLine(20, 40, 250, 40)
pen.setStyle(Qt.DashLine)
painter.setPen(pen)
painter.drawLine(20, 60, 250, 60)
pen.setStyle(Qt.DashDotDotLine)
painter.setPen(pen)
painter.drawLine(20, 80, 250, 80)
pen.setStyle(Qt.DotLine)
painter.setPen(pen)
painter.drawLine(20, 100, 250, 100)
pen.setStyle(Qt.DashDotDotLine)
painter.setPen(pen)
painter.drawLine(20, 120, 250, 120)
pen.setStyle(Qt.CustomDashLine)
pen.setDashPattern([1, 4, 5, 4])
painter.setPen(pen)
painter.drawLine(20, 140, 250, 140)
painter.end()
if __name__ == '__main__':
app = QApplication(sys.argv)
main = DrawMultiLine()
print(main.__doc__)
main.show()
sys.exit(app.exec_())
结果
|