You could delete and recreate all the comboboxes everytime your spinbox value changes. It may not be the most efficient, but it's quite simple.
Just have a list with references to the labels/comboxes as an attribute. In your slot, call deleteLater()
on each item, then delete the reference by setting your list to []
. Finally, recreate the items, add them to your layout and repopulate your list.
Also, you should have a look at New Style Signals and Slots. They are nicer than the ugly C++ style connect.
class DynamicComboBoxes(QtGui.QWidget):
def __init__(self, parent=None):
super(DynamicComboBoxes, self).__init__(parent)
vbox = QtGui.QVBoxLayout(self)
spinbox = QtGui.QSpinBox(self)
spinbox.setRange(0,10)
spinbox.valueChanged.connect(self.onChangeValue)
vbox.addWidget(spinbox)
self.grid = QtGui.QGridLayout()
self.itemlist = []
vbox.addLayout(self.grid)
vbox.addStretch(1)
def onChangeValue(self, val):
for label, combobox in self.itemlist:
label.deleteLater()
combobox.deleteLater()
self.itemlist = []
for i in range(val):
label = QtGui.QLabel('This is Label {}'.format(i))
combobox = QtGui.QComboBox()
self.grid.addWidget(label, i, 0)
self.grid.addWidget(combobox, i, 1)
self.itemlist.append([label, combobox])