[英]Mouseover event filter for a PyQT Label
I've been trying to convert the example here to work with a simple label. 我一直在尝试将此处的示例转换为使用简单标签。
Here's the code: 这是代码:
class mouseoverEvent(QtCore.QObject):
def __init__(self, parent):
super(mouseoverEvent, self).__init__(parent)
def eventFilter(self, object, event):
if event.type() == QtCore.QEvent.MouseMove:
print "mousemove!"
self.filter = mouseoverEvent(self)
self.label.installEventFilter(self.filter)
Now curiously, this actually works, but not without my console being spammed with "mousemove!" 现在奇怪的是,它确实有效,但是在我的控制台中没有“ mousemove!”垃圾邮件的情况下 (good) as well as the error: TypeError: invalid result type from mouseoverEvent.eventFilter()
(良好)以及错误: TypeError:mouseoverEvent.eventFilter()中无效的结果类型
I've not quite figured out the complex relationship between events yet, so this is a bit greek to me. 我还没有弄清楚事件之间的复杂关系,所以对我来说有点希腊。 So, what gives?
那么,有什么用呢?
Thanks in advance. 提前致谢。
我相信您需要从eventFilter返回True
或False
,以指示您是否已完全处理事件。
Check out what I just discovered. 查看我刚刚发现的内容。 This is a snippet from some actual code, so class names are specific in my instance.
这是一些实际代码的片段,因此类名称在我的实例中是特定的。
def mouseMoveEvent(self, event=None):
if self.activeLayer.layerName != 'Whiteboard': super(MapPage, self).mouseMoveEvent(event)
else:
if (event.buttons() & Qt.LeftButton) and self.scribbling:
self.drawLineTo(event.scenePos())
What I have done is re-declared the mouseMoveEvent, but if the running instance of the activeLayer is not named 'Whiteboard' then the software runs through an 'original' mouseMoveEvent. 我所做的是重新声明了mouseMoveEvent,但是如果activeLayer的运行实例未命名为“ Whiteboard”,则该软件将通过“原始” mouseMoveEvent运行。
class mouseoverEvent(QtCore.QObject): def init (self, parent): super(mouseoverEvent, self). class mouseoverEvent(QtCore.QObject):定义init (自身,父级):super(mouseoverEvent,自身)。 init (parent)
初始化 (父)
def eventFilter(self, object, event):
if event.type() == QtCore.QEvent.MouseMove:
print "mousemove!"
return super(mouseoverEvent, self).eventFilter(object, event)
self.filter = mouseoverEvent(self) self.label.installEventFilter(self.filter) self.filter = mouseoverEvent(self)self.label.installEventFilter(self.filter)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.