1

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_())
Axel Foley
  • 29
  • 7
  • 1
    The information you gave are insufficient, as the code you shared should work. Can you provide a [minimal, reproducible example](https://stackoverflow.com/help/minimal-reproducible-example)? – musicamante Oct 20 '19 at 01:37
  • 3
    add `self.comboBox1.blockSignals(True)` before `self.comboBox1.clear()` and add `self.comboBox1.blockSignals(False)` after `self.comboBox1.addItems(['item1','item2']` – eyllanesc Oct 20 '19 at 16:38

0 Answers0