1 给窗口加过滤器
def init_wnd(self):
self.installEventFilter(self) # 1 安装事件过滤器
# 2 重写事件过滤器, 所有的事件都会走这里经过
def eventFilter(self, obj: QObject, event: QEvent) -> bool:
print(obj, event)
if obj is self:
if event.type() == QEvent.MouseButtonPress:
print("鼠标按下")
elif event.type() == QEvent.MouseButtonRelease:
print("鼠标释放")
elif event.type() == QEvent.MouseMove:
print("鼠标移动")
return super(WndClientLogin, self).eventFilter(obj, event)
2 给控件加过滤器
def __init__(self):
self.lbe_captcha_pic.installEventFilter(self) # 给验证码标签安装事件过滤器
def eventFilter(self, obj: QObject, ev: QEvent):
if obj is self.lbe_captcha_pic:
ev_type = ev.type()
if ev_type == QEvent.MouseButtonPress or ev_type == QEvent.KeyPress:
self.popup_captcha_wnd()
self.show_info("验证码已刷新")
return super().eventFilter(obj, ev)