tags:

views:

247

answers:

1

I want to use a QComboBox with the "keys" and "values" from a tuple similar to the ones used in a django models. For example I have the following structure for a person's sex.

SEX_CHOICES = (('M', 'Male'), ('F', 'Female'))

The first item of the tuple contains the code of the sex that is stored in the database, and the second one the text that I want to display in the QComboBox as an item.

Is there a way in wich I could set the QComboBox value as M and it displays Male. An also when the user select the element Male I could get the selected value as M.

Thanks

A: 

Use

cb.addItem  ( text, userData )

and pass the DB key as userData. If you need to change the current selection, use cb.itemData() to get the DB key of each item and compare it to the one you need.

Alternatively, record the indexes as you add items in a Python map and use this to directly look up the correct index.

To make things more easy to use, wrap the QComboBox with a Python class that offers setters and getters for the current DB key and which hides the mapping.

Aaron Digulla
Thanks, I think it will be easy to implement. The only problem that I will have is using the custom QComboBox in Qt Designer. Is there an easy way to add the new control?Do you think that it will be easier to use a qt model?, I have already check it and it can hold the mapping that you mention. But still having problems setting the value from the db value.
Danilo
See the docs for how to build widgets in PyQt that can be used in the designer: http://www.riverbankcomputing.co.uk/static/Docs/PyQt4/pyqt4ref.html#using-python-widgets-in-qt-designer
Aaron Digulla