Discussion:
[pyqtgraph] GraphicsLayoutWidget.removeItem() doesn't affect index of other items in widget?
Dan
2014-11-21 21:03:53 UTC
Permalink
I'm running into a bit of an issue with the indexing of plots added to the
GraphicsLayoutWidget.

I basically have a plotting app that has a list (using QComboBox) of all of
the plots that were created along with the actually widget that contains
the plots.

So, for example, if I had 4 plots, which are stacked on top of each other,
I have a combobox with the items:

Plot1
Plot2
Plot3
Plot4

Those items have indexes:
(0,0)
(1,0)
(2,0)
(3,0)

since I'm adding them using nextRow()

In the combo box, the index values are 0, 1, 2, 3.

My app allows the user to remove a plot by selecting it in the combo box. I
pass the index of the combo box to

item = GraphicsLayoutWidget.getItem(combo_box_index, 0)

And then use

GraphicsLayoutWidget.removeItem(item)


This works on the first time that the clear plot button is pressed.
However, because I update the combo box on removing a plot I'm running into
an index issue.

So, for example, if I remove plot 3, the combo box now has index values 0,
1, 2 (where "Plot 4" is now in the 2 index position vs. its previous spot
in the 3 position)

If I then go to remove Plot4, I get an error of:

Exception: Could not determine index of item None


It seems that the GraphicsLayoutWidget does not update the index value of
the other items in the widget if a plot is removed from the widget.

I.e.
Rather than Plot1, Plot2, and Plot4 having index: (0,0), (1,0), (2,0) they
return their original indexing of (0,0), (1,0), (3,0)

Is this correct? Is there any way to return the index values of all the
items currently in the GraphicsLayoutWidget?


relevant bits of code:

self.plotWidget = pg.GraphicsLayoutWidget(Form)
self.plotWidget.setObjectName("plotWidget")


def add_new_plot(self):
name = "Plot" + str(self.counter)
#plt = self.plotWidget.addPlot(row=self.counter, col=0, name=name,
title=name)
plt = self.plotWidget.addPlot(name=name, title=name)
self.plotWidget.nextRow()
self.plot_data(plt)
plt.setXLink("Plot1")
self.plotNum_dropdown.addItem(name)
self.clearPlot_dropdown.addItem(name)
self.counter += 1

def clear_plot(self):
index = self.clearPlot_dropdown.currentIndex()
if index == 0:
self.plotWidget.clear()
self.plotNum_dropdown.clear()
self.clearPlot_dropdown.clear()
self.clearPlot_dropdown.addItem("All")
self.counter = 1
elif index > 0:
plt = self.plotWidget.getItem(index, 0)
self.plotWidget.removeItem(plt)
self.clearPlot_dropdown.removeItem(index)
self.plotNum_dropdown.removeItem(index)
--
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 pyqtgraph+***@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/pyqtgraph/f1f97312-e272-42ba-8efa-5a3be224b23e%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Dan
2014-11-22 23:12:44 UTC
Permalink
So I have confirmed that this is what is happening (i.e. that the indexes
of the other plots are not changed when a plot of removed). I have a bit of
a workaround for my usage by just keeping a list of the indexes (just the
row index number, since the column index is 0), which I can then index into
using the index return from the combo box.

This has left me with some undesirable effects, though, so I have a couple
of questions still:

1. Again, is there any way to return a list of the plot items in a graphics
layout widget? I know I can return graphicslayoutwidget.items(), but this
returns a lot more than just the plot items.

2. Is there any way to rename a plot after it has been created? Or return
the name of a plot? With a user adding and removing plots I need a way to
update the plot names, otherwise I get lists like this:

Plot1, Plot3, Plot4, Plot7

etc., since I can't figure out a way to just loop through all the plots and
rename them based on the number of plots current in the widget.
--
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 pyqtgraph+***@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/pyqtgraph/1abab68b-af6c-41c4-8597-c9709358e642%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
mrkrynmdsco
2016-05-05 12:10:19 UTC
Permalink
Hi Dan,

I had a similar problem, wherein my widget allows to add a PlotItem object
in a GraphicsLayout. Each of my plot have a close button added and bundled
together as a class.

..the class of bundled button and plot..

class PlotTag(QtCore.QObject):
def __init__(self):
super(PlotTag, self).__init__()

self.plot = Plot()
self.closeBtn = Button()
self.closeBtn.setParentItem(self.plot)
delIcon = pg.QtGui.QPixmap('gui/delete_01.png').scaled(8, 8,
pg.QtCore.Qt.KeepAspectRatio)
self.closeBtn.setPixmap(delIcon)

x = self.plot.size().width() + 5
self.closeBtn.setPos(x, 0)

..and here is my SubPlotLayout class

class SubPlotLayout(pg.GraphicsLayout):

def __init__(self, withAddPlot=False):
super(SubPlotLayout, self).__init__()

self.setContentsMargins(5, 5, 5, 5)

if withAddPlot:
self.setupAddPlotBtn()
else:
self.addPlotBtn = None

def setupAddPlotBtn(self):
self.addPlotBtn = Button()
self.addPlotBtn.setParentItem(self)
addIcon = pg.QtGui.QPixmap('gui/addRow_01.png').scaled(16, 16,
pg.QtCore.Qt.KeepAspectRatio)
self.addPlotBtn.setPixmap(addIcon)
# Button Position
self.addPlotBtn.setPos(5, 5)
# Signal Connection
self.addPlotBtn.clicked.connect(self.addPlotBtnClicked)

def addPlotBtnClicked(self):
plot = PlotTag()
self.nextRow()
self.addItem(plot.plot)

# The SECRET!!!!!
cbk = partial(self.plotClose, plot.plot)
plot.closeBtn.clicked.connect(cbk)

print('Created ' + str(self.itemIndex(plot.plot)))

def plotClose(self, plot):
index = self.itemIndex(plot)
print('Closed Plot Row ' + str(index))
# Remove from the SubPlotLayout
self.removeItem(plot)

Hope you could solve your problem as well. :)
--
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 pyqtgraph+***@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/pyqtgraph/fecfe76d-290a-44f5-89f2-d0e65e753625%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Loading...