如何在 PyQt5 中捕捉悬停和鼠标离开信号 [英] How to Catch Hover and Mouse Leave Signal In PyQt5
问题描述
QPushButton 有一个名为 clicked()
的信号,我们可以通过它捕捉点击事件.有没有捕获悬停和离开事件的方法或信号?
QPushButton has a signal which is named clicked()
, and we can catch click events through it. Is there a method or signal which catches hover and leave events?
如何捕捉鼠标悬停按钮和鼠标离开按钮,如下所示:
How can I catch mouse-over button and mouse-leave button, like this:
button = QPushButton(window)
button.clicked.connect(afunction)
注意:我使用的是python3.
Note: I use python3.
推荐答案
您需要继承 QPushButton
类并重新实现 enterEvent
和 leaveEvent
>:
You need to subclass the QPushButton
class and reimplement the enterEvent
and leaveEvent
:
class Button(QPushButton):
def __init__(self, parent=None):
super(Button, self).__init__(parent)
# other initializations...
def enterEvent(self, QEvent):
# here the code for mouse hover
pass
def leaveEvent(self, QEvent):
# here the code for mouse leave
pass
然后您可以在本地处理该事件,或发出信号(如果其他小部件需要对此事件做出反应,您可以使用信号将该事件通知其他小部件).
You can then handle the event locally, or emit a signal (if other widgets needs to react on this event you could use a signal to notify the event to other widgets).
这篇关于如何在 PyQt5 中捕捉悬停和鼠标离开信号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!