[PyQt] Double Click event from QPushButton

lj larry at foxgulch.com
Thu Jul 24 23:21:17 BST 2008


Previous version for detecting a QPushButton double click was messy.
This version using a single shot is much cleaner.

Larry
----------------------------------------------------------------------------
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from PyQt4 import QtCore, QtGui
import  sys

class Ui_Dialog(object):
    def setupUi(self, Dialog):
        Dialog.setObjectName("Dialog")

Dialog.resize(QtCore.QSize(QtCore.QRect(0,0,335,166).size()).expandedTo(Dialog.minimumSizeHint()))

        self.pushButton = QtGui.QPushButton(Dialog)
        self.pushButton.setGeometry(QtCore.QRect(100,50,80,28))
        self.pushButton.setObjectName("pushButton")
        self.retranslateUi(Dialog)
        QtCore.QMetaObject.connectSlotsByName(Dialog)

    def retranslateUi(self, Dialog):
        Dialog.setWindowTitle(QtGui.QApplication.translate("Dialog",
"Dialog", None, QtGui.QApplication.UnicodeUTF8))
        self.pushButton.setText(QtGui.QApplication.translate("Dialog",
"PushButton", None, QtGui.QApplication.UnicodeUTF8))


class TestDlg(QDialog, Ui_Dialog):
    def __init__(self, parent=None):
        super(TestDlg, self).__init__(parent)
        self.setupUi(self)
        self.connect(self.pushButton, SIGNAL("clicked()"),
self.pb_DetectDoubleClick)
        self.timer = QTimer()
        self.timer.setInterval(500)
        self.timer.setSingleShot(True)

    def pb_DetectDoubleClick(self):
        #if after receiving the first click, the timer isn't running,
start the timer and return
        if  not self.timer.isActive():
            self.timer.start()
            print "return"
            return
        # if timer is running and hasn't timed out,  the second click
occured within timer interval
        if self.timer.isActive() : 
            print "DoubleClick"
            self.timer.stop()   # stop timer so next click can start it
again
            self.doDoubleClickAction
            
    def  doDoubleClickAction(self):
        print "Double Click on pushButton detected"
            
   
if __name__ == "__main__":
    app = QApplication(sys.argv)
    form = TestDlg()
    form.show()
    app.exec_()



More information about the PyQt mailing list