2016-08-01 3 views
0

저는 PyQt4에서 인터페이스를 프로그래밍 중이며 QLabels를 사용하고 mousepressevent 기능을 사용하여 클릭 할 수있게 만들고 있습니다. 같은 mousepressevent 슬롯을 가진 여러 레이블 (신호)이 있습니다. 여기 내가하려는 일의 요지가있다.PyQt4의 발신자 검색 QLabel 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

신호를 보내기 위해 버튼을 사용할 수 없습니까? 단추의 스타일 시트를 변경하여 레이블처럼 평평하게 보이게 할 수 있습니다. – 101

+0

이 프로그램에서는 버튼이 아닌 레이블이 필요했습니다. 그리고 Brendan의 설명을 가져 주셔서 감사합니다. –

답변

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)