1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 | from PyQt4 import QtCore, QtGui class CustomButton(QtGui.QPushButton): left_clicked = QtCore.pyqtSignal( int ) right_clicked = QtCore.pyqtSignal( int ) def __init__( self , * args, * * kwargs): QtGui.QPushButton.__init__( self , * args, * * kwargs) self .timer = QtCore.QTimer() self .timer.setInterval( 250 ) self .timer.setSingleShot( True ) self .timer.timeout.connect( self .timeout) self .left_click_count = self .right_click_count = 0 def mousePressEvent( self , event): if event.button() = = QtCore.Qt.LeftButton: self .left_click_count + = 1 if not self .timer.isActive(): self .timer.start() if event.button() = = QtCore.Qt.RightButton: self .right_click_count + = 1 if not self .timer.isActive(): self .timer.start() def timeout( self ): if self .left_click_count > = self .right_click_count: self .left_clicked.emit( self .left_click_count) else : self .right_clicked.emit( self .right_click_count) self .left_click_count = self .right_click_count = 0 class MyDialog(QtGui.QDialog): def __init__( self , parent = None ): super (MyDialog, self ).__init__(parent) self .button1 = CustomButton( "Button 1" ) hbox = QtGui.QHBoxLayout() hbox.addWidget( self .button1) self .setLayout(hbox) self .button1.left_clicked[ int ].connect( self .left_click) self .button1.right_clicked[ int ].connect( self .right_click) def left_click( self , nb): if nb = = 1 : print ( 'Single left click' ) else : print ( 'Double left click' ) def right_click( self , nb): if nb = = 1 : print ( 'Single right click' ) else : print ( 'Double right click' ) if __name__ = = '__main__' : import sys app = QtGui.QApplication(sys.argv) w = MyDialog() w.show() sys.exit(app.exec_()) |