简体   繁体   中英

Detecting mouseReleaseEvent in pyqtgraph. Problem with inheritance

Pyqtgraph does unfortunately not provide a mouseRelease signal.

Therefore, I would like to modify the mouseReleaseEvent method in pyqtgraphs GraphicsScene class to emit a custom signal.

But in my example below, the mouseReleaseEvent function overrides the equivalent method in the QWidget parent and not in pyqtgraph as desired. How can I address and change this method or is there an easier way for detecting a mouse button release?

import sys, pyqtgraph
from PyQt5 import QtGui, QtWidgets

class Window(QtWidgets.QDialog):
    def __init__(self, parent=None):
        QtWidgets.QWidget.__init__(self)
        layout = QtGui.QGridLayout(self)
        view = pyqtgraph.GraphicsLayoutWidget()
        layout.addWidget(view,0,0)
        view.scene().sigMouseClicked.connect(self.OnClick)
    def OnClick(self):
        print("click") # This works inside the GraphicsLayoutWidget.
    def mouseReleaseEvent(self,ev): # This does only work outside the pyqtgraph widget. It overrides the method in QWidget and not in pyqtgraph.GraphicsScene()
        print("released ",ev)

if __name__ == '__main__':
    app = QtWidgets.QApplication(sys.argv)
    form = Window()
    form.show()
    sys.exit(app.exec_())

So, I don't know if this is particularly clever, but I came up with a solution using a separate timer that keeps track of the existence of clickEvents . I hope this is helpful for people having a similar issue.

import sys, pyqtgraph
from PyQt5 import QtGui, QtWidgets, QtCore

class Window(QtWidgets.QDialog):
    def __init__(self, parent=None):
        QtWidgets.QWidget.__init__(self)
        layout = QtGui.QGridLayout(self)
        self.view = pyqtgraph.GraphicsLayoutWidget()
        self.timer = QtCore.QTimer()
        layout.addWidget(self.view,0,0)
        self.proxy = pyqtgraph.SignalProxy(self.view.scene().sigMouseMoved, rateLimit=30, slot=self.OnMouseMove)
        self.view.scene().sigMouseClicked.connect(self.release)
        self.timer.timeout.connect(self.release)
    def release(self):
        if not self.view.scene().clickEvents:
            print("release after drag")
            self.timer.stop()
        elif not self.timer.isActive():
            print("release after click")
    def OnMouseMove(self):
        if not self.timer.isActive() and self.view.scene().clickEvents:
            self.timer.start(10) # After a drag release, this is the "wait" time before self.release is called.
    def mouseReleaseEvent(self,ev): # This does only work outside the pyqtgraph widget. It overrides the method in QWidget and not in pyqtgraph.GraphicsScene()
        print("released ",ev)

if __name__ == '__main__':
    app = QtWidgets.QApplication(sys.argv)
    form = Window()
    form.show()
    sys.exit(app.exec_())

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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