2016-10-06 6 views
0

私はしばらくの間OpenCV GUI関数を使っていましたが、その可能性はPythonユーザーにとっては少し制限されています。今日私はPyqtを使い始めて、次の結論に達しました。qtは本当に混乱しています。 OpenCVのでマウスイベントの扱いopencv gui vs pyqt

私は次のようにします:今

マウスイベントに関する質問は

import cv2 
cv2.namedWindow('Window',1) 
def CallBackFunc(event,x,y,flags,param): 
    global xc,yc,evt,flg 
    xc,yc,evt,flg=x,y,event,flags 

cv2.setMouseCallback('Window', CallBackFunc) 

これは、常にグローバル変数xc,yc,evt,flgをリフレッシュする別々のスレッドを開き、私はどこでもそれらにアクセスすることができますいつでも私は欲しい。私はリフレッシュを停止したい場合は、私はちょうどcv2.setMouseCallback('Window',nothing)は、nothing

def nothing(): 
    pass 

ができることは、マウスイベントに対処するための最も美しい方法ではないかもしれないが、私はそれで元気です。 PyQtでこのような自由をどうやって実現できますか?

EDIT:

は、たとえば、次のスクリプトは、白い円が表示され、常にそれにテキストを描画します。

import sys 
from PySide import QtGui 
import numpy as np 
import cv2 

class QCustomLabel (QtGui.QLabel): 


    def __init__ (self, parent = None): 
     super(QCustomLabel, self).__init__(parent) 
     self.setMouseTracking(True) 


    def mouseMoveEvent (self, eventQMouseEvent): 
     self.x,self.y=eventQMouseEvent.x(),eventQMouseEvent.y() 
     cvImg=np.zeros((900,900),dtype=np.uint8) 
     cv2.circle(cvImg,(449,449),100,255,-1) 
     cv2.putText(cvImg,"x at {}, y at {}".format(self.x,self.y),(375,455), cv2.FONT_HERSHEY_SIMPLEX,0.5,(0,0,0),1,cv2.LINE_AA) 
     height, width= cvImg.shape 
     bytearr=cvImg.data 
     qImg = QtGui.QImage(bytearr, width, height, QtGui.QImage.Format_Indexed8) 
     self.setPixmap(QtGui.QPixmap.fromImage(qImg)) 

    def mousePressEvent (self, eventQMouseEvent): 
     self.evt=eventQMouseEvent.button() 


class QCustomWidget (QtGui.QWidget): 
    def __init__ (self, parent = None): 
     super(QCustomWidget, self).__init__(parent) 
     self.setWindowOpacity(1) 
     # Init QLabel 
     self.positionQLabel = QCustomLabel(self) 
     # Init QLayout 
     layoutQHBoxLayout = QtGui.QHBoxLayout() 
     layoutQHBoxLayout.addWidget(self.positionQLabel) 

     self.setLayout(layoutQHBoxLayout) 
     self.show() 


if QtGui.QApplication.instance() is not None: 
    myQApplication=QtGui.QApplication.instance() 
else: 
    myQApplication = QtGui.QApplication(sys.argv) 


myQTestWidget = QCustomWidget() 
myQTestWidget.show() 
myQApplication.exec_() 

ここでの問題は、QCustomLabelクラス内で、またMouseMoveEvent関数内で実行されるという点です。しかし、別の機能が必要なので、そのクラスの外にあるdrawCircleと呼んで、マウスの位置とイベントにアクセスできます。 opencvでは、これは全く問題ありません。そして、それはpyqtの実装に必要な書き込み作業のほんの一部にすぎません。

私は正しい質問があると思います:私はまだpyqtが好きではないのですか?

+0

あなたはPySide、ないPyQtはを使用しています。しかしどちらの方法でも、ある言語/ライブラリ/フレームワークを別の言語/フレームワークと同じように動作させることは常に間違いです。 – ekhumoro

答えて

0

あなたはQLabelをサブクラス化することを避けるために、イベント・フィルターを使用することができます。

class QCustomWidget (QtGui.QWidget): 
    def __init__ (self, parent = None): 
     super(QCustomWidget, self).__init__(parent) 
     self.setWindowOpacity(1) 
     # Init QLabel 
     self.positionQLabel = QtGui.QLabel(self) 
     self.positionQLabel.setMouseTracking(True) 
     self.positionQLabel.installEventFilter(self) 
     # Init QLayout 
     layoutQHBoxLayout = QtGui.QHBoxLayout() 
     layoutQHBoxLayout.addWidget(self.positionQLabel)  
     self.setLayout(layoutQHBoxLayout) 
     self.show() 

    def eventFilter(self, source, event): 
     if event.type() == QtCore.QEvent.MouseMove: 
      self.drawCircle(event.x(), event.y()) 
     return super(QCustomWidget, self).eventFilter(source, event) 

    def drawCircle(self, x, y): 
     # whatever 
+0

ありがとうございます。私の知る限り、opencvs guiの機能はqt上に構築されています。したがって、両方のフレームワークで同様の実装を導き出すことは当然のようでした。 –

+0

@AntonAlice。私は重複が多いと確信していますが、明らかにいくつかのことが異なるように動作します。私はあなたがqtが柔軟性をたくさん提供していることがわかると思いますが、時にはそれはより詳細な実装を犠牲にしています。 – ekhumoro

+0

QtGui._Label(自己) –

関連する問題