treatment of mouse events opencv gui vs pyqt
<p>I was working with OpenCV gui functions for a while, and the possibilities are a little restricting for python users. Today I started with Pyqt and come across the following conclusion: qt is really confusing. </p>

<p>Now the question concerning mouse events:</p>

<p>In OpenCV I just do the following:</p>

<pre><code>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)
</code></pre>

<p>This opens a seperate thread, which constantly refreshes the global variables <code>xc,yc,evt,flg</code>, and I can access them anywhere, at anytime I want. If I want to stop the refreshing, I just do a <code>cv2.setMouseCallback('Window',nothing)</code>, whereby <code>nothing</code> is</p>

<pre><code>def nothing():
  pass
</code></pre>

<p>It may not be the most beautiful way of dealing with mouse events, but I am fine with it. How can I achieve such freedom with PyQt?</p>

<p>EDIT:</p>

<p>For example, the following script is displaying a white circle, and constantly drawing a text into it. </p>

<pre><code>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_()
</code></pre>

<p>The problem here is, that this is all executed inside the QCustomLabel Class, and inside the MouseMoveEvent function. But I want a seperate function, lets call it <code>drawCircle</code>, outside of that class, which has access to the mouse position and events. With opencv this would be no problem at all. And it would take only a fraction of the writing effort, which is needed for a pyqt implementation.</p>

<p>I think the right question is: Why dont I like pyqt yet?</p>

<p>You can use an event-filter to avoid having to subclass the <code>QLabel</code>:</p>

<pre><code>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
</code></pre>

python opencv mouseevent pyqt4