0

我正在设置一个自定义 PlotDataItem 来接收 mouseDragEvents。我已经根据我的需要调整了这个答案。现在我刚刚在事件中添加了一个简单的 setData 来检查它是否正常工作。自定义 PlotDataItem 如下:

class CustomPlotItem(pg.PlotDataItem):
    def __init__(self, *args, **kargs):
        super().__init__(*args, **kargs)

    def setParentItem(self, parent):
        super().setParentItem(parent)
        self.parentBox = self.parentItem().parentItem()      

    def mouseDragEvent(self, ev):
        if ev.button() != QtCore.Qt.LeftButton:
            ev.ignore()
            return

        if ev.isStart():
            if self.parentBox.curveDragged != None or not self.mouseShape().contains(ev.pos()):
                ev.ignore()
                return
            self.parentBox.curveDragged = self            
        elif ev.isFinish():
            self.parentBox.curveDragged = None
            return
        elif self.parentBox.curveDragged != self:
            ev.ignore()
            return

        self.setData([40,50,60,200],[20,50,80,500])
        ev.accept()

PlotDataItem 被添加到实现curveDragged的​​自定义 ViewBox 中,因此我知道正在拖动哪条曲线(如果有)。出于调试目的,我还禁用了 ViewBox 的 mouseDragEvents。

但是,当尝试拖动 ViewBox 中的线时,没有任何反应。此外,如果我在 mouseDragEvent 顶部添加异常,则不会发生任何事情。这让我相信 mouseDragEvent 根本没有被调用。

我使用 Python 3.3(Anaconda Distribution)和 pyqtgraph 的开发版本(0.9.9)。

我希望有人可以帮助我:)。提前致谢。

4

1 回答 1

2

PlotDataItem是 aPlotCurveItem和 a的包装器ScatterPlotItem。因此,它实际上没有任何图形或它自己的可点击形状。我会尝试创建一个子类PlotCurveItem。如果您确实需要使用PlotDataItem,则可以对其进行修改,使其从包裹曲线继承其形状:

class CustomPlotItem(pg.PlotDataItem):
    def __init__(self, *args, **kargs):
        super().__init__(*args, **kargs)
        # Need to switch off the "has no contents" flag
        self.setFlags(self.flags() & ~self.ItemHasNoContents)

    def mouseDragEvent(self, ev):
        print("drag")
        if ev.button() != QtCore.Qt.LeftButton:
            ev.ignore()
            return

        if ev.isStart():
            print("start")
        elif ev.isFinish():
            print("finish")

    def shape(self):
        # Inherit shape from the curve item
        return self.curve.shape()

    def boundingRect(self):
        # All graphics items require this method (unless they have no contents)
        return self.shape().boundingRect()

    def paint(self, p, *args):
        # All graphics items require this method (unless they have no contents)
        return

    def hoverEvent(self, ev):
        # This is recommended to ensure that the item plays nicely with 
        # other draggable items
        print("hover")
        ev.acceptDrags(QtCore.Qt.LeftButton)
于 2014-04-29T22:18:36.063 回答