2016-12-15 3 views
0

イベントが発生したウィジェットはどのようにして見つけることができますか?その位置は、その下にあるウィジェットの起点に相対的です。イベントを1つのウィジェットだけに制限したい。 eventFilterを通過さPyQt4マウス移動イベント:ウィジェットを見つける

def eventFilter(self, source, event): 
    if event.type() == QtCore.QEvent.MouseMove: 
     if event.buttons() == QtCore.Qt.NoButton: 
      pos = event.pos() 
      self.statusbar.showMessage("mouse tracked at {} x {}".format(pos.x(), pos.y())) 
      print(dir(event)) 
      # self.edit.setText('x: %d, y: %d' % (pos.x(), pos.y())) 
    return QtGui.QMainWindow.eventFilter(self, source, event) 

答えて

1

イベントは、明示的にイベント・フィルターを設置したウィジェットに制限されています。 1つのウィジェットだけがイベントフィルタをインストールした場合、source引数は1つのウィジェットになることができます。複数のウィジェットが同じフィルタリング対象上のイベント・フィルターをインストールした場合

、あなただけ使用することができ、それらを区別するためにIDをチェック:

def eventFilter(self, source, event): 
    if (event.type() == QtCore.QEvent.MouseMove and 
     event.buttons() == QtCore.Qt.NoButton and 
     source is self.myInterestingWidget): 
     # do stuff with event... 
     print(event.pos()) 
    return QtGui.QMainWindow.eventFilter(self, source, event) 
関連する問題