我在PyQt4中编写了一个接口,我正在使用QLabel并使用mousepressevent函数使它们可以点击。我有多个标签(信号)具有相同的mousepressevent插槽。以下是我尝试做的事情的要点。
class Example(QtGui.QWidget):
def __init__(self):
super(Example, self).__init__()
self.initUI()
def initUI(self):
lbl1=QtGui.QLabel(self)
lbl2=QtGui.QLabel(self)
lbl1.mousePressEvent=self.exampleMousePress
lbl2.mousePressEvent=self.exampleMousePress
def exampleMousePress(self,event):
print "The sender is: " + sender().text()
问题是发件人功能在这里不起作用。有没有办法在exampleMousePress函数中获取事件发送者?
谢谢大家!
答案 0 :(得分:0)
您可以使用event-filtering执行此操作:
class Example(QtGui.QWidget):
def __init__(self):
super(Example, self).__init__()
self.initUI()
def initUI(self):
lbl1 = QtGui.QLabel(self)
lbl2 = QtGui.QLabel(self)
lbl1.installEventFilter(self)
lbl2.installEventFilter(self)
def eventFilter(self, source, event):
if event.type() == QtCore.QEvent.MouseButtonPress:
print "The sender is:", source.text()
return super(Example, self).eventFilter(source, event)