为了账号安全,请及时绑定邮箱和手机立即绑定

在 pyqtgraph 中检测 mouseReleaseEvent。继承问题

在 pyqtgraph 中检测 mouseReleaseEvent。继承问题

莫回无 2021-07-19 20:02:50
不幸的是,Pyqtgraph 不提供 mouseRelease 信号。因此,我想修改mouseReleaseEventpyqtgraphsGraphicsScene类中的方法以发出自定义信号。但是在我下面的示例中, mouseReleaseEvent 函数会根据需要覆盖QWidget父级中的等效方法,而不是 pyqtgraph 中的等效方法。如何解决和更改此方法,或者是否有更简单的方法来检测鼠标按钮释放?import sys, pyqtgraphfrom PyQt5 import QtGui, QtWidgetsclass 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_())
查看完整描述

1 回答

?
LEATH

TA贡献1936条经验 获得超6个赞

所以,我不知道这是否特别聪明,但我想出了一个使用单独的计时器来跟踪clickEvents. 我希望这对有类似问题的人有所帮助。


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


查看完整回答
反对 回复 2021-07-27
  • 1 回答
  • 0 关注
  • 220 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信