0

As far as I can see from examples on the Net, such as http://zetcode.com/gui/pyqt5/widgets2/ - a QComboBox needs to have .addItem() called, so it actually adds an item in the dropdown.

So, if I have a Python list of options, in principle, I'd have to iterate over it, and to .addItem on each item, for instance:

self.combo = QComboBox(self)

self.my_choices = ["Choice 1", "Choice A", "Choice 300" ]

for tchoice in self.my_choices:
  self.combo.addItem(tchoice)

So, I'd just like to confirm - is there some sort of a mechanism, whereby I could specify that the combobox "binds" with a list - so that, afterwards, whenever I change the list (append to it, or remove from it, or change its items), the combo box content changes as well, with no further commands? (Otherwise, in principle, I have to clean the combobox entries, then repeat the above loop again, upon every change of the list).

If there is no such mechanism, just a "No" would be a very acceptable answer - I just wanted to confirm this.

sdbbs
  • 4,270
  • 5
  • 32
  • 87
  • 1
    Yes it is possible -- although you might have to create your own version (aka sub-class QComboBox) -- however you should dig a bit deeper into the documentation as QComboBox has methods such as `.addItems( QStringList )` as well as `.insertItem(Index, String)` so there is more than one way to update the contents of a QComboBox – Dennis Jensen Nov 06 '19 at 15:22
  • Thanks @DennisJensen - much appreciated! Also, the Q this is a duplicate to has a post that directly refers to "binding". – sdbbs Nov 06 '19 at 15:37
  • Well I am not a proponent to "binding" unless it is needed as it can introduce other issues that are sometimes harder to deal with than doing it straight up -- but then again it does have its uses and this might be one -- just be sure that before you introduce that complexity that you are sure that it is the best path to take – Dennis Jensen Nov 06 '19 at 17:02

0 Answers0