繁体   English   中英

鼠标事件的处理opencv gui vs pyqt

[英]treatment of mouse events opencv gui vs pyqt

我使用OpenCV gui函数已经有一段时间了,对于python用户来说,这种可能性有些局限。 今天,我从Pyqt开始,并得出以下结论:qt确实令人困惑。

现在有关鼠标事件的问题:

在OpenCV中,我只需执行以下操作:

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)nothingnothing

def nothing():
  pass

它可能不是处理鼠标事件的最漂亮方法,但是我对此很满意。 如何使用PyQt实现这种自由?

编辑:

例如,以下脚本显示一个白色圆圈,并不断在其中绘制文本。

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?

您可以使用事件过滤器来避免必须对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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM