[PyQt] Double Click event from QPushButton

lj larry at foxgulch.com
Thu Jul 24 21:31:40 BST 2008


Ref post "[PyQt] Double Click event from QPushButton" 

The attached doesn't seem very clean but seems to work never the less.
About 500 msec seems about right to catch most  double clicks on the
pushButton. 

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.pb_firstRelease = QTime(0, 0, 0, 0)
        self.pb_secondRelease = QTime(0, 0, 0, 0)

    def pb_DetectDoubleClick(self):
        #first click, set time of click  then  return and wait for
second pb release
        if self.pb_firstRelease == QTime(0, 0, 0, 0):
            self.pb_firstRelease = QTime.currentTime()
            print "return"
            return
        # another click with time in first slot 
        if self.pb_firstRelease > 0 : 
            self.pb_secondRelease = QTime.currentTime()
            if (1000 *
self.pb_secondRelease.second()+self.pb_secondRelease.msec()) - (1000*
self.pb_firstRelease.second() + self.pb_firstRelease.msec()) <500:
                self.DoubleClickAction()
        self.pb_firstRelease = QTime(0, 0, 0, 0)
        print "reset and return"
        return
            
    def DoubleClickAction(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