I'm trying to put together a fairly simple app that allows the user to add 
and/or remove individual points on top of a data already plotted. 

A simplified version of the app is shown below. Basically individual points 
are plotted at the nearest point along the already-plotted curve when the 
user right clicks on the curve. If the user then double clicks (which I've 
achieved by just using a timer, since PlotDataItem.sigClicked doesn't 
provide information about the mouse click event - if there's a better way 
to do this I'd love to hear about that as well) on that point it is removed 
from the plot. 

The way I've gone about doing this, though, is causing me to have the 
following error: 

"RuntimeError: wrapped C/C++ object of type ScatterPlotItem has been 
deleted"

As a work around I'm keeping a list of the PlotDataItem objects, but 
obviously that's a pretty asinine solution. Is there a more correct way to 
go about achieving this? 


Code: 

import sys
import time
from PyQt4 import QtGui
import pyqtgraph as pg
import numpy as np


class ExPlot(QtGui.QWidget):
    def __init__(self):
        super().__init__()
        desktop = QtGui.QDesktopWidget()
        width = desktop.screenGeometry().width()
        ratio = width / 1920
        self.resize(1400*ratio, 800*ratio)
        
        self.layout=QtGui.QHBoxLayout(self)
        self.plotWidget = pg.GraphicsLayoutWidget(self)
        self.layout.addWidget(self.plotWidget)
        self.plot = self.plotWidget.addPlot(1,1, enableMenu=False)

        self.indexes = {}
        self.counter = 1
        self.time = 0
        x = np.arange(0, 10, 0.01)
        y = np.sin(x)

        plotData = self.plot.plot(x, y)
        self.plotWidget.scene().sigMouseClicked.connect(self.addPoint)
        
    def addPoint(self, event):
        if event.button()==2:
            items = self.plotWidget.scene().items(event.scenePos())
            plot = items[0]
            try:
                data = plot.getData()
                vb = items[1]
                index = int(vb.mapSceneToView(event.scenePos()).x() / 0.01)
                
                
                if index not in self.indexes.values():
                    x = data[0][index]
                    y = data[1][index]
                    point = self.plot.plot([x, ], [y, ], symbol='o',
                                           symbolSize=20, clickable=True,
                                           name=self.counter)
                    
                    point.sigClicked.connect(self.removePoint)
                    self.indexes[self.counter] = index
                    self.counter += 1
            
            #these are thrown if user clicks way to the left/right of the 
plotted data
            except (AttributeError, IndexError):
                pass
    
    def removePoint(self, item):
        tdiff = time.time() - self.time
        if tdiff < 1:
            del self.indexes[item.name()]
            self.plot.removeItem(item)
        
        self.time = time.time()
    
if __name__ == '__main__':
    app = QtGui.QApplication(sys.argv)
    ex = ExPlot()
    ex.show()
    sys.exit(app.exec_())
        

-- 
You received this message because you are subscribed to the Google Groups 
"pyqtgraph" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/pyqtgraph/9f28a746-1851-4e5f-8c21-607ce33b8045%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to