Anton Alice Anton Alice - 2 months ago 6
Python Question

treatment of mouse events opencv gui vs pyqt

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.

Now the question concerning mouse events:

In OpenCV I just do the following:

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)


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

def nothing():
pass


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?

EDIT:

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

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_()


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
drawCircle
, 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.

I think the right question is: Why dont I like pyqt yet?

Answer

You can use an event-filter to avoid having to subclass the QLabel:

class QCustomWidget (QtGui.QWidget):
    def __init__ (self, parent = None):
        super(QCustomWidget, self).__init__(parent)
        self.setWindowOpacity(1)
        # Init QLabel
        self.positionQLabel = QtGui.Label(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