在 pyqtgraph 中检测 mouseReleaseEvent。继承问题

不幸的是,Pyqtgraph 不提供 mouseRelease 信号。


因此,我想修改mouseReleaseEventpyqtgraphsGraphicsScene类中的方法以发出自定义信号。


但是在我下面的示例中, mouseReleaseEvent 函数会根据需要覆盖QWidget父级中的等效方法,而不是 pyqtgraph 中的等效方法。如何解决和更改此方法,或者是否有更简单的方法来检测鼠标按钮释放?


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


莫回无
浏览 227回答 1
1回答

LEATH

所以,我不知道这是否特别聪明,但我想出了一个使用单独的计时器来跟踪clickEvents. 我希望这对有类似问题的人有所帮助。import sys, pyqtgraphfrom PyQt5 import QtGui, QtWidgets, QtCoreclass 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_())
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python