I suspect that the consensus would be "don't do that" -- having
single-click and double click perform unrelated actions. But here's an
implementation based on the advice at
http://www.qtcentre.org/threads/7858-Double-Click-Capturing
(use Ctrl-Break to break out of the event loop)
import PyQt4.QtCore as C
import PyQt4.QtGui as G
class Button(G.QPushButton):
def __init__(self, text):
G.QPushButton.__init__(self, text)
# flag to suppress second mouseReleaseEvent
# in this double-click event sequence:
# 1. mousePressEvent
# 2. mouseReleaseEvent
# 3. mouseDoubleClickEvent
# 4. mouseReleaseEvent
self.double_clicked = False
def mouseReleaseEvent(self, evt):
# executed for first mouseReleaseEvent
if not self.double_clicked:
self.first_click_timer = C.QTimer()
self.first_click_timer.setSingleShot(True)
# double-click must occur within 1/4 second of first-click
release
self.first_click_timer.setInterval(250)
self.first_click_timer.timeout.connect(self.single_click_action)
self.first_click_timer.start()
# executed for second mouseReleaseEvent
else:
# reset the flag
self.double_clicked = False
def single_click_action(self):
print "Performing single-click action"
def mouseDoubleClickEvent(self, evt):
# suppress the single-click action; perform double-click action
instead
self.first_click_timer.stop()
print "Performing double-click action"
# prepare for second mouseReleaseEvent
self.double_clicked = True
# main program
app = G.QApplication([])
button = Button("Click or double-click me")
button.show()
app.exec_()
HTH,
John