| 18 | |
| 19 | |
| 20 | class ZoomButton(QPushButton): |
| 21 | |
| 22 | def __init__(self, *args, **kwargs): |
| 23 | super(ZoomButton, self).__init__(*args, **kwargs) |
| 24 | self._animation = QPropertyAnimation( |
| 25 | self, b'geometry', self, duration=200) |
| 26 | |
| 27 | def updatePos(self): |
| 28 | # 记录自己的固定的geometry值 |
| 29 | self._geometry = self.geometry() |
| 30 | self._rect = QRect( |
| 31 | self._geometry.x() - 6, |
| 32 | self._geometry.y() - 2, |
| 33 | self._geometry.width() + 12, |
| 34 | self._geometry.height() + 4 |
| 35 | ) |
| 36 | |
| 37 | def showEvent(self, event): |
| 38 | super(ZoomButton, self).showEvent(event) |
| 39 | self.updatePos() |
| 40 | |
| 41 | def enterEvent(self, event): |
| 42 | super(ZoomButton, self).enterEvent(event) |
| 43 | self._animation.stop() # 停止动画 |
| 44 | # 修改动画的开始值 |
| 45 | self._animation.setStartValue(self._geometry) |
| 46 | # 修改动画的终止值 |
| 47 | self._animation.setEndValue(self._rect) |
| 48 | self._animation.start() |
| 49 | |
| 50 | def leaveEvent(self, event): |
| 51 | super(ZoomButton, self).leaveEvent(event) |
| 52 | self._animation.stop() # 停止动画 |
| 53 | # 修改动画的开始值 |
| 54 | self._animation.setStartValue(self._rect) |
| 55 | # 修改动画的终止值 |
| 56 | self._animation.setEndValue(self._geometry) |
| 57 | self._animation.start() |
| 58 | |
| 59 | def mousePressEvent(self, event): |
| 60 | self._animation.stop() # 停止动画 |
| 61 | # 修改动画的开始值 |
| 62 | self._animation.setStartValue(self._rect) |
| 63 | # 修改动画的终止值 |
| 64 | self._animation.setEndValue(self._geometry) |
| 65 | self._animation.start() |
| 66 | super(ZoomButton, self).mousePressEvent(event) |
| 67 | |
| 68 | |
| 69 | class Window(QWidget): |