MCPcopy Index your code
hub / github.com/PyQt5/PyQt / PushButtonLine

Class PushButtonLine

QPushButton/BottomLineProgress.py:50–110  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

48
49
50class PushButtonLine(QPushButton):
51 lineColor = QColor(0, 150, 136)
52
53 def __init__(self, *args, **kwargs):
54 self._waitText = kwargs.pop("waitText", "等待中")
55 super(PushButtonLine, self).__init__(*args, **kwargs)
56 self._text = self.text()
57 self._percent = 0
58 self._timer = QTimer(self, timeout=self.update)
59 self.clicked.connect(self.start)
60
61 def __del__(self):
62 self.stop()
63
64 def paintEvent(self, event):
65 super(PushButtonLine, self).paintEvent(event)
66 if not self._timer.isActive():
67 return
68 # 画进度
69 painter = QPainter(self)
70 pen = QPen(self.lineColor)
71 pen.setWidth(4)
72 painter.setPen(pen)
73 painter.drawLine(0, self.height(), self.width()
74 * self._percent, self.height())
75
76 def start(self):
77 if hasattr(self, "loadingThread"):
78 return self.stop()
79 self.loadingThread = LoadingThread(self)
80 self.loadingThread.valueChanged.connect(self.setPercent)
81 self._timer.start(100) # 100ms
82 self.loadingThread.start()
83 self.setText(self._waitText)
84
85 def stop(self):
86 try:
87 if hasattr(self, "loadingThread"):
88 if self.loadingThread.isRunning():
89 self.loadingThread.requestInterruption()
90 self.loadingThread.quit()
91 self.loadingThread.wait(2000)
92 del self.loadingThread
93 except RuntimeError:
94 pass
95 try:
96 self._percent = 0
97 self._timer.stop()
98 self.setText(self._text)
99 except RuntimeError:
100 pass
101
102 def setPercent(self, v):
103 self._percent = v
104 if v == 1:
105 self.stop()
106 self.update()
107

Callers 1

__init__Method · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected