窗口抖动动画 :param target: 目标控件
(self, target)
| 31 | |
| 32 | # 下面这个方法可以做成这样的封装给任何控件 |
| 33 | def doShakeWindow(self, target): |
| 34 | """窗口抖动动画 |
| 35 | :param target: 目标控件 |
| 36 | """ |
| 37 | if hasattr(target, '_shake_animation'): |
| 38 | # 如果已经有该对象则跳过 |
| 39 | return |
| 40 | |
| 41 | animation = QPropertyAnimation(target, b'pos', target) |
| 42 | target._shake_animation = animation |
| 43 | animation.finished.connect(lambda: delattr(target, '_shake_animation')) |
| 44 | |
| 45 | pos = target.pos() |
| 46 | x, y = pos.x(), pos.y() |
| 47 | |
| 48 | animation.setDuration(200) |
| 49 | animation.setLoopCount(2) |
| 50 | animation.setKeyValueAt(0, QPoint(x, y)) |
| 51 | animation.setKeyValueAt(0.09, QPoint(x + 2, y - 2)) |
| 52 | animation.setKeyValueAt(0.18, QPoint(x + 4, y - 4)) |
| 53 | animation.setKeyValueAt(0.27, QPoint(x + 2, y - 6)) |
| 54 | animation.setKeyValueAt(0.36, QPoint(x + 0, y - 8)) |
| 55 | animation.setKeyValueAt(0.45, QPoint(x - 2, y - 10)) |
| 56 | animation.setKeyValueAt(0.54, QPoint(x - 4, y - 8)) |
| 57 | animation.setKeyValueAt(0.63, QPoint(x - 6, y - 6)) |
| 58 | animation.setKeyValueAt(0.72, QPoint(x - 8, y - 4)) |
| 59 | animation.setKeyValueAt(0.81, QPoint(x - 6, y - 2)) |
| 60 | animation.setKeyValueAt(0.90, QPoint(x - 4, y - 0)) |
| 61 | animation.setKeyValueAt(0.99, QPoint(x - 2, y + 2)) |
| 62 | animation.setEndValue(QPoint(x, y)) |
| 63 | |
| 64 | animation.start(animation.DeleteWhenStopped) |
| 65 | |
| 66 | |
| 67 | if __name__ == '__main__': |
no test coverage detected