I've got a QListWidget that I have populated with multiple instances of an item generated from a custom widget. This custom widget contains a QComboBox. I have it so the plot at the bottom updates when any of the QComboBoxes are changed, but how do I make the plot update when the user selects a particular row in my list? I can't figure out how to get the current value of the QComboBox for the selected item.
class MainWidget(QWidget):
def showselection(self):
# get current selection from QComboBox for selected item
# run self.bias_graph.updateplot passing in current selection
def __init__(self, parent):
super().__init__(parent)
grid = QGridLayout()
self.bias_graph = BiasGraph(self, width=5, height=4)
self.multiplexerList = QListWidget()
for i in range(10):
item = QListWidgetItem(self.multiplexerList)
itemWidget = MultiplexerListItem(self.bias_graph, i)
item.setSizeHint(itemWidget.sizeHint())
self.multiplexerList.addItem(item)
self.multiplexerList.setItemWidget(item, itemWidget)
self.multiplexerList.itemSelectionChanged.connect(self.showselection)
heading1 = QLabel('Multiplexer')
heading2 = QLabel('Waveform')
heading1.setFixedHeight(50)
heading2.setFixedHeight(50)
grid.addWidget(heading1, 0, 0)
grid.addWidget(heading2, 0, 1)
grid.addWidget(self.multiplexerList, 1, 0, 1, 2)
bottomleft = self.bias_graph
grid.addWidget(bottomleft, 11, 0)
self.setLayout(grid)
self.show()
class MultiplexerListItem(QWidget):
def __init__(self, main_plot, i, parent = None):
super().__init__(parent)
grid = QGridLayout()
grid.addWidget(QLabel(str(i + 1)), 0, 0)
waveselect = QComboBox()
waveselect.addItem("A")
waveselect.addItem("B")
waveselect.activated[str].connect(main_plot.updateplot)
grid.addWidget(waveselect, 0, 1)
self.setLayout(grid)
class BiasGraph(FigureCanvas):
def __init__(self, parent = None, width=5, height=4, dpi=100):
fig = Figure(figsize = (width, height), dpi = dpi)
self.axes = fig.add_subplot(111)
FigureCanvas.__init__(self, fig)
self.setParent(parent)
FigureCanvas.setSizePolicy(self,
QSizePolicy.Expanding,
QSizePolicy.Expanding)
FigureCanvas.updateGeometry(self)
default = []
A = [0, 0, -1, -2, -3, -2, -1, 0, 0]
B = [0, 0, 1, 2, 3, 2, 1, 0, 0]
self.data = {"A" : A, "B" : B}
self.plot(default)
def updateplot(self, text):
self.plot(self.data[text])
def plot(self, X):
self.axes.clear()
ax = self.figure.add_subplot(111)
ax.plot(X, 'r-')
ax.set_title('Bias Waveform')
self.draw()