这对我来说很有效:
# coding: utf-8
import sys
from PyQt4 import QtCore, QtGui
class MyDialog(QtGui.QDialog):
def __init__(self, parent=None):
super(MyDialog, self).__init__(parent)
self.button1 = QtGui.QPushButton("Button 1")
self.button2 = QtGui.QPushButton("Button 2")
hbox = QtGui.QHBoxLayout()
hbox.addWidget(self.button1)
hbox.addWidget(self.button2)
self.setLayout(hbox)
self.button1.clicked.connect(self.on_button_clicked)
self.button2.clicked.connect(self.on_button_clicked)
self.button1.installEventFilter(self)
def eventFilter(self, obj, event):
if event.type() in (QtCore.QEvent.MouseButtonPress,
QtCore.QEvent.MouseButtonDblClick):
if event.button() == QtCore.Qt.LeftButton:
print "left"
return True
return super(MyDialog, self).eventFilter(obj, event)
def on_button_clicked(self):
print('on_button_clicked')
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
w = MyDialog()
w.show()
sys.exit(app.exec_())