Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add items to a QComboBox in PyQt/PySide

I need some help adding some items to a QComboBox. So I have two comboboxes, and one populates the other depending on the item selected.

My question is that, using additem for new items, it works, but if I choose another option for the combobox, it adds the new items, but the previous items are gone - and there are blank items below the new ones.

I thought that each time I chose a new option from the first combobox to clear the contents of the second combobox. So I used the clear() on the second - but it didn't work.

That's how I thought of it :

self.comboBox_2.clear() for index,i in enumerate(list1):   self.comboBox_2.addItem(_fromUtf8(""))   self.comboBox_2.setItemText(index+2, QApplication.translate("Dialog", i, None, QApplication.UnicodeUTF8)) 

The above is part of a function that executes when the first combobox changes.

like image 435
IordanouGiannis Avatar asked Dec 07 '11 19:12

IordanouGiannis


People also ask

How do I add items to QComboBox?

A combobox can be populated using the insert functions, insertItem() and insertItems() for example. Items can be changed with setItemText(). An item can be removed with removeItem() and all items can be removed with clear().

How do I add items to QComboBox in Python?

A combobox can be created with the QComboBox class. Items are added using the class method addItem, followed by a string parameter. Sometimes its called a listbox, as it shows a list of items a user can select from. If you want a user to choose from a set of items, the listbox is the best option.

How do I create a drop down menu in PyQt?

First, we define the comboBox, then we add a bunch of options, then we move the comboBox (a drop down button). When the combo box has a choice, it will take the string version of the choice, and run self.


2 Answers

Assuming list1 is a list of strings, then you can simply add them all at once using the addItems method:

self.comboBox_2.clear() self.comboBox_2.addItems(list1) 

Note that you are probably using QApplication.translate in the wrong way in your example. If you want to make it possible for the strings in list1 to be translated into a different language, you should do that when you create the the list, and use string literals.

For example:

list1 = [     self.tr('First Item'),     self.tr('Second Item'),     self.tr('Third Item'),     ] 

Also note that the _fromUtf8 function is only really useful if you're using string literals containing non-ascii characters in your code - otherwise, it's basically a no-op.

EDIT

If your list contains, say, tuples of pixmaps and text, then you can use something like this:

self.comboBox_2.clear() for pixmap, text in list1:     self.comboBox_2.addItem(QIcon(pixmap), text) 
like image 69
ekhumoro Avatar answered Oct 18 '22 18:10

ekhumoro


There are some simple and easy to read demos/examples here https://github.com/shuge/Enjoy-Qt-Python-Binding ,

clone it and you will find a demo about how to use basic QComboBox and custom its icon item.

like image 44
Tony Avatar answered Oct 18 '22 20:10

Tony