0

I tried to delete all comboBox items using a loop as follow:

for idx in range(comboBox.count()): 
     print(idx, comboBox.count()) # show the loop number and check how many items left each loop
     comboBox.removeItem(idx)

However, still 2 items left after the looping through all items instead of clearing them all out.

The prints are as following.

0 5
1 4
2 3
3 2
4 2
5 2

So it went haywire after 3. Why is this happening?

Jason Gao
  • 1
  • 2
  • 2
    Imagine you have a list with 3 items, ["a", "b", "c"], then you cycle from 0 to 2 to remove them: you first remove index 0, so now you have ["b", "c"]; but then you remove index 1, what is left? Use [`comboBox.clear()`](https://doc.qt.io/qt-5/qcombobox.html#clear), alternatively always remove the first or last index if you need to track what you're removing, or cycle with a reversed range. – musicamante Mar 03 '22 at 13:18

0 Answers0