| 50 | |
| 51 | |
| 52 | class RubberBandButton(QPushButton): |
| 53 | |
| 54 | def __init__(self, *args, **kwargs): |
| 55 | self._bgcolor = QColor(kwargs.pop("bgcolor", Qt.green)) |
| 56 | super(RubberBandButton, self).__init__(*args, **kwargs) |
| 57 | self.setFlat(True) |
| 58 | self.setCursor(Qt.PointingHandCursor) |
| 59 | self._width = 0 |
| 60 | self._height = 0 |
| 61 | |
| 62 | def paintEvent(self, event): |
| 63 | self._initAnimate() |
| 64 | painter = QStylePainter(self) |
| 65 | painter.setRenderHint(QPainter.Antialiasing, True) |
| 66 | painter.setRenderHint(QPainter.HighQualityAntialiasing, True) |
| 67 | painter.setRenderHint(QPainter.SmoothPixmapTransform, True) |
| 68 | painter.setBrush(QColor(self._bgcolor)) |
| 69 | painter.setPen(QColor(self._bgcolor)) |
| 70 | painter.drawEllipse( |
| 71 | QRectF( |
| 72 | (self.minimumWidth() - self._width) / 2, |
| 73 | (self.minimumHeight() - self._height) / 2, |
| 74 | self._width, |
| 75 | self._height, |
| 76 | ) |
| 77 | ) |
| 78 | # 绘制本身的文字和图标 |
| 79 | options = QStyleOptionButton() |
| 80 | options.initFrom(self) |
| 81 | size = options.rect.size() |
| 82 | size.transpose() |
| 83 | options.rect.setSize(size) |
| 84 | options.features = QStyleOptionButton.Flat |
| 85 | options.text = self.text() |
| 86 | options.icon = self.icon() |
| 87 | options.iconSize = self.iconSize() |
| 88 | painter.drawControl(QStyle.CE_PushButton, options) |
| 89 | event.accept() |
| 90 | |
| 91 | def _initAnimate(self): |
| 92 | if hasattr(self, "_animate"): |
| 93 | return |
| 94 | self._width = self.minimumWidth() * 7 / 8 |
| 95 | self._height = self.minimumHeight() * 7 / 8 |
| 96 | # self._width=175 |
| 97 | # self._height=175 |
| 98 | |
| 99 | # 宽度动画 |
| 100 | wanimate = QPropertyAnimation(self, b"rWidth") |
| 101 | wanimate.setEasingCurve(QEasingCurve.OutElastic) |
| 102 | wanimate.setDuration(700) |
| 103 | wanimate.valueChanged.connect(self.update) |
| 104 | |
| 105 | # 插入宽度线性值 |
| 106 | wanimate.setKeyValueAt(0, self._width) |
| 107 | # wanimate.setKeyValueAt(0.1, 180) |
| 108 | # wanimate.setKeyValueAt(0.2, 185) |
| 109 | # wanimate.setKeyValueAt(0.3, 190) |