Python 在PyQT5中处理事件
我目前正在使用PyQt制作的触摸屏和GUI。我在eventFilter函数中实现了自己的滑动方法。我的问题是,当函数返回False时,TouchEnd事件根本无法识别。当我返回True时,两个按钮都被识别,但是按钮对touchevents根本没有反应(我认为这是因为当函数返回True时,mouseevents被过滤掉了)。关于如何正确实现eventfilter的任何提示Python 在PyQT5中处理事件,python,qt,pyqt,Python,Qt,Pyqt,我目前正在使用PyQt制作的触摸屏和GUI。我在eventFilter函数中实现了自己的滑动方法。我的问题是,当函数返回False时,TouchEnd事件根本无法识别。当我返回True时,两个按钮都被识别,但是按钮对touchevents根本没有反应(我认为这是因为当函数返回True时,mouseevents被过滤掉了)。关于如何正确实现eventfilter的任何提示 def eventFilter(self, source, event): print(event.type())
def eventFilter(self, source, event):
print(event.type())
if event.type() == QEvent.TouchBegin:
self.startpoint_x = event.touchPoints()[0].pos().x()
self.startpoint_y = event.touchPoints()[0].pos().y()
event.accept()
print("debug1")
elif event.type() == QEvent.TouchEnd: # Catch the TouchEnd event.
event.accept()
self.endpoint_x = event.touchPoints()[0].pos().x()
self.endpoint_y = event.touchPoints()[0].pos().y()
if self.endpoint_x > self.startpoint_x + 50 and self.endpoint_y > self.startpoint_y - 100 and self.endpoint_y < self.startpoint_y + 100:
print("Swipe Left Detected")
elif self.endpoint_x < self.startpoint_x - 50 and self.endpoint_y > self.startpoint_y - 50 and self.endpoint_y < self.startpoint_y + 50:
print("Swipe Right Detected")
return False
def eventFilter(自身、源、事件):
打印(event.type())
如果event.type()==QEvent.TouchBegin:
self.startpoint_x=event.touchPoints()[0].pos().x()
self.startpoint_y=event.touchPoints()[0].pos().y()
event.accept()
打印(“调试1”)
elif event.type()==QEvent.TouchEnd:#捕获TouchEnd事件。
event.accept()
self.endpoint_x=event.touchPoints()[0].pos().x()
self.endpoint_y=event.touchPoints()[0].pos().y()
如果self.endpoint_x>self.startpoint_x+50和self.endpoint_y>self.startpoint_y-100和self.endpoint_yself.startpoint_y-50和self.endpoint_y
在touch事件分支中,您应该返回True
(表示您已处理它们),然后返回super().eventFilter(source,event)
,用于其他所有内容。接受触摸事件是没有意义的,因为你没有传播它们。@Ekhumaro是的,这是有效的,但随后会出现其他问题。我的按钮在触摸事件中似乎根本没有反应。按钮似乎只对mouseEvents起作用,但似乎eventFilters会过滤所有mouseEvents。