PyQt(PySide)とOpenCVで画像処理その5(イベントフィルター)
前回の続きということで、マウス操作にてカーソル位置の取得を行う。 PyQt(PySide)とOpenCVで画像処理その4(右クリックメニュー) - tataboxの備忘録
イベントフィルターを使用する。 前回のコードに下記を追加
def __init__(self, parent = None): ・ ・ ・ #ここから #イベントフィルターの追加 self.pic_View.installEventFilter(self) #イベントフィルター時に実施する関数 def eventFilter(self, source, event): #画像表示部の情報をマウスボタンを押す際に取得 if (event.type() == QtCore.QEvent.MouseButtonPress and source is self.pic_View): #右クリックはコンテキストメニュー用に例外処理 if event.button() == QtCore.Qt.RightButton: pass else: #マウス位置の取得 pos = event.pos() #メッセージボックスで表示 msgbox = QtGui.QMessageBox(self) msgbox.setText('mouse position: (%d, %d)' % (pos.x(), pos.y())) #メッセージボックスの表示 ret = msgbox.exec_() #戻り値が必要 return QtGui.QWidget.eventFilter(self, source, event)
左orホイールクリックで、メッセージボックスにカーソルの座標が表示される
ホイールに拡大縮小、左クリックに元倍率、右クリックにコンテキストメニューを割り振る予定。
参考
python - PyQt: how to handle event without inheritance - Stack Overflow