pyqt5 draw a dotted line

Solutions on MaxInterview for pyqt5 draw a dotted line by the best coders in the world

showing results for - "pyqt5 draw a dotted line"
Valentino
06 May 2016
1
2from PyQt5.QtWidgets import QWidget, QApplication
3from PyQt5.QtGui import QPainter, QPen
4from PyQt5.QtCore import Qt
5import sys
6
7
8class Example(QWidget):
9
10    def __init__(self):
11        super().__init__()
12
13        self.initUI()
14
15    def initUI(self):
16        self.setGeometry(300, 300, 280, 270)
17        self.setWindowTitle('Pen styles')
18        self.show()
19
20    def paintEvent(self, e):
21        qp = QPainter()
22        qp.begin(self)
23        self.drawLines(qp)
24        qp.end()
25
26    def drawLines(self, qp):
27        pen = QPen(Qt.black, 2, Qt.SolidLine)
28
29        qp.setPen(pen)
30        qp.drawLine(20, 40, 250, 40)
31
32        pen.setStyle(Qt.DashLine)
33        qp.setPen(pen)
34        qp.drawLine(20, 80, 250, 80)
35
36        pen.setStyle(Qt.DashDotLine)
37        qp.setPen(pen)
38        qp.drawLine(20, 120, 250, 120)
39
40        pen.setStyle(Qt.DotLine)
41        qp.setPen(pen)
42        qp.drawLine(20, 160, 250, 160)
43
44        pen.setStyle(Qt.DashDotDotLine)
45        qp.setPen(pen)
46        qp.drawLine(20, 200, 250, 200)
47
48        pen.setStyle(Qt.CustomDashLine)
49        pen.setDashPattern([1, 4, 5, 4])
50        qp.setPen(pen)
51        qp.drawLine(20, 240, 250, 240)
52
53
54def main():
55    app = QApplication(sys.argv)
56    ex = Example()
57    sys.exit(app.exec_())
58
59
60if __name__ == '__main__':
61    main()
62