| 25 | |
| 26 | |
| 27 | class ColourfulProgress(QProgressBar): |
| 28 | |
| 29 | def __init__(self, *args, **kwargs): |
| 30 | self._color = kwargs.pop('color', QColor(43, 194, 83)) |
| 31 | self._fps = kwargs.pop('fps', 60) |
| 32 | self._lineWidth = kwargs.pop('lineWidth', 50) # 线条宽度 |
| 33 | self._radius = kwargs.pop('radius', None) # None为自动计算圆角 |
| 34 | self._animation = None |
| 35 | super(ColourfulProgress, self).__init__(*args, **kwargs) |
| 36 | self.setColor(self._color) |
| 37 | self.setFps(self._fps) |
| 38 | self.setLineWidth(self._lineWidth) |
| 39 | self.setRadius(self._radius) |
| 40 | |
| 41 | def setColor(self, color): |
| 42 | """ |
| 43 | :type color: QColor |
| 44 | :param color: 颜色 |
| 45 | """ |
| 46 | self._color = QColor(color) if isinstance( |
| 47 | color, (QColor, Qt.GlobalColor)) else QColor(43, 194, 83) |
| 48 | |
| 49 | def setFps(self, fps): |
| 50 | """ |
| 51 | :type fps: int |
| 52 | :param fps: 帧率 |
| 53 | """ |
| 54 | self._fps = max(int(fps), 1) if isinstance(fps, (int, float)) else 60 |
| 55 | |
| 56 | def setLineWidth(self, width): |
| 57 | """ |
| 58 | :type width: int |
| 59 | :param width: 线条宽度 |
| 60 | """ |
| 61 | self._lineWidth = max(int(width), 0) if isinstance(width, |
| 62 | (int, float)) else 50 |
| 63 | |
| 64 | def setRadius(self, radius): |
| 65 | """ |
| 66 | :type radius: int |
| 67 | :param radius: 半径 |
| 68 | """ |
| 69 | self._radius = max(int(radius), 1) if isinstance(radius, |
| 70 | (int, float)) else None |
| 71 | |
| 72 | def paintEvent(self, _): |
| 73 | """ |
| 74 | 重写绘制事件,参考 qfusionstyle.cpp 中的 CE_ProgressBarContents 绘制方法 |
| 75 | """ |
| 76 | option = QStyleOptionProgressBar() |
| 77 | self.initStyleOption(option) |
| 78 | |
| 79 | painter = QPainter(self) |
| 80 | painter.setRenderHint(QPainter.Antialiasing) |
| 81 | painter.translate(0.5, 0.5) |
| 82 | |
| 83 | vertical = option.orientation == Qt.Vertical # 是否垂直 |
| 84 | inverted = option.invertedAppearance # 是否反转 |