我有一个eventFilter在我的定制标签,我想闻一下双击它。这个是可能的吗?
self.installEventFilter(self)
# Handles mouse events
def eventFilter(self, object, event):
try:
if event.buttons() == QtCore.Qt.LeftButton:
#LeftButton event
else:
# nothing is being pressed
except:
pass发布于 2014-09-02 14:55:06
是的,这是可能的,但由于一些奇怪的原因,这并不是那么简单。当然,您永远也不知道一次单击之后是否会出现另一次单击,从而有效地导致双击。这就是为什么一定有一些内在的等待时间。Qt这样做并为双击传递事件(QEvent.MouseButtonDblClick)。另一方面,Qt仍然提供单次单击(QEvent.MouseButtonPress)的事件,即使在双击的情况下也是如此,但只有一个。这可能不是最好的设计。
我们必须正确地区分它们。我是用一个额外的计时器来做的,这个计时器需要比内置的Qt计时器长一点来检测双击。然后,守则是:
from PySide import QtCore, QtGui
class MyLabel(QtGui.QLabel):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.installEventFilter(self)
self.single_click_timer = QtCore.QTimer()
self.single_click_timer.setInterval(200)
self.single_click_timer.timeout.connect(self.single_click)
def single_click(self):
self.single_click_timer.stop()
print('timeout, must be single click')
def eventFilter(self, object, event):
if event.type() == QtCore.QEvent.MouseButtonPress:
self.single_click_timer.start()
return True
elif event.type() == QtCore.QEvent.MouseButtonDblClick:
self.single_click_timer.stop()
print('double click')
return True
return False
app = QtGui.QApplication([])
window = MyLabel('Click me')
window.resize(200, 200)
window.show()
app.exec_()https://stackoverflow.com/questions/25206464
复制相似问题