Trying to remove all items in a combobox and load a new list of items after an index change from other comboboxes. This is a very simple version of this project. I've tried this a number of approaches. The only way I can get it to work (see the commented block) not work with the logic of the program. The example here will crash the UI.
from PyQt5 import QtWidgets, uic, QtCore
class MyWindow(QtWidgets.QMainWindow):
def __init__(self):
super(MyWindow,self).__init__()
uic.loadUi('a-frame.ui',self)
self.setWindowTitle('a-frame')
self.comboBox1.currentIndexChanged.connect(lambda: combobox_changed(self))
self.comboBox2.currentIndexChanged.connect(lambda: combobox_changed(self))
# ->>> This block will crash the program
def combobox_changed(self):
combo_a = self.comboBox1.currentIndex()
if combo_a == 1:
self.comboBox1.clear()
self.comboBox1.addItems(['item1','item2'])
# ->>> This block works but it won't work cause the number of
# indexes in the combobox vary with each choice
# def combobox_changed(self):
# combo_a = self.comboBox1.currentIndex()
# combo_b = self.comboBox1.currentIndex()
# if combo_a == 1:
# self.comboBox1.clear()
# elif combo_b == 0:
# self.comboBox1.addItems(['item1', 'item2'])
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
window = MyWindow()
window.show()
sys.exit(app.exec_())