【问题标题】:PyQtGraph Custom PlotDataItem is not receiving mouseDragEventsPyQtGraph 自定义 PlotDataItem 没有收到 mouseDragEvents
【发布时间】:2014-06-15 03:10:21
【问题描述】:

我正在设置一个自定义 PlotDataItem 来接收 mouseDragEvents。我已经根据我的需要调整了this answer。现在我刚刚在事件中添加了一个简单的 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)。

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

【问题讨论】:

    标签: python pyqtgraph


    【解决方案1】:

    PlotDataItemPlotCurveItemScatterPlotItem 的包装器。因此,它实际上没有任何图形或它自己的可点击形状。我会尝试创建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)
    

    【讨论】:

    • 非常感谢。我想我会坚持使用 PlotDataItem,因为我以后需要能够根据散点是否可见来调整 mouseEvent。我已经实施了你的建议,它就像一个魅力。
    猜你喜欢
    • 1970-01-01
    • 2021-10-10
    • 2017-02-03
    • 2019-08-01
    • 1970-01-01
    • 2016-08-17
    • 2011-08-22
    • 2022-11-03
    • 2014-04-26
    相关资源
    最近更新 更多