Question

I'm setting up a custom PlotDataItem to recieve mouseDragEvents. I've adjusted this answer to my needs. For now I've just added a simple setData to the event to check if it's working. The custom PlotDataItem is as:

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

The PlotDataItem is added to a custom ViewBox this implements curveDragged, so I know which curve is being dragged, if any. I've also disabled the ViewBox's mouseDragEvents for debugging purposes.

However when try to drag the line in the ViewBox, nothing happens. Also if I add an exception at the top of the mouseDragEvent nothing happens. This leads me to believe mouseDragEvent is not being called at all.

Im using Python 3.3 (Anaconda Distribution) and the develop version (0.9.9) of pyqtgraph.

I hope someone can help me with this :). Thanks in advance.

Was it helpful?

Solution

PlotDataItem is a wrapper around a PlotCurveItem and a ScatterPlotItem. As such, it does not actually have any graphics or a clickable shape of its own. I would try making a subclass of PlotCurveItem instead. If you really need to use PlotDataItem, then it is possible to modify it such that it inherits its shape from the wrapped curve:

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)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top