self.sendClickEvent(cev[0]): mouseReleaseEvent 时出错

self.sendClickEvent(cev[0]): error when mouseReleaseEvent

我的代码如下。当我双击时,出现如上所示的错误。这种情况怎么办?

我找到了一些线索

  1. http://www.pyqtgraph.org/documentation/_modules/pyqtgraph/GraphicsScene/mouseEvents.html#MouseClickEvent.double
  2. http://python.6.x6.nabble.com/problem-with-mouseDoubleClickEvent-on-QTreeView-1-td1921227.html

我猜 pg.Plotwidget 没有 'mouseDoubleClickEvent',所以问题可能是继承 class...

from PyQt5 import QtGui
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *

import pyqtgraph as pg

class MyPlot(pg.PlotWidget):
    def mouseDoubleClickEvent(self, ev):
        print(1)

class main(QWidget):
    def __init__(self):
        super().__init__()
        pw = MyPlot() 
        pw.showGrid(x=True, y=True)
        pw.setXRange(0, 10)
        x = range(0, 10)
        y = [2, 4, 6, 8, 10, 12, 14, 16, 18, 20]
        layout = QHBoxLayout()
        pw.plot(x, y, pen='b', symbol='x', symbolPen='g', symbolBrush=0.2, name='green')
        layout.addWidget(pw)
        self.setLayout(layout)
        self.show()

if __name__ == "__main__":
    import sys
    app = QApplication(sys.argv)
    app.setAttribute(Qt.AA_Use96Dpi)
    main = main()
    main.show()

    sys.exit(app.exec_())
Traceback (most recent call last):
  File "C:\Python34\lib\site-packages\pyqtgraph\GraphicsScene\GraphicsScene.py", line 199, in mouseReleaseEvent
    if self.sendClickEvent(cev[0]):
IndexError: list index out of range

问题是重写mouseDoubleClickEvent 方法而不是通过super 调用父方法导致传输到使用它的其他方法的事件是None 例如mouseReleaseEvent 导致错误。解决方法是使用super.

调用父类的方法
class MyPlot(pg.PlotWidget):
    def mouseDoubleClickEvent(self, ev):
        print(1)
        <b>super().mouseDoubleClickEvent(ev)</b>

pyqtgraph没有预见到这种情况可以认为是一个错误,而且pyqtgraph开发人员似乎已经知道了,因为他们已经在pyqtgraph的开发分支中解决了它,所以另一种解决方案是使用该代码并用于您可以使用以下过程安装它:

git clone -b develop git@github.com:pyqtgraph/pyqtgraph.git
sudo python setup.py install