[PyKDE] Re: Using comboboxes indexes for referencing data
Jim Bublitz
jbublitz at nwinternet.com
Tue May 10 23:43:59 BST 2005
On Tuesday 10 May 2005 14:44, Jorge Godoy wrote:
> Jim Bublitz <jbublitz at nwinternet.com> writes:
> > In the case of QComboBox, "index" refers to an item's position in the
> > combo box's list of items. The list of items is a list of strings.
>
> Pity. :-( I was happy thinking I could do that as with wxPython.
>
> > There are a number of ways to do what you want, but they would be things
> > like maintaining a separate data structure which maps items and (your)
> > index values, or subclassing QComboBox to do that for you automatically.
> > The implementation would depend a lot on your application.
> I'll have to take a look at this. For now, all cases contained unique
> values, so I could get the text and find the correct reference to it, but I
> would really like having this possibility.
> Do you think that using a dict would be enough? The key would be the
> position in the combobox and the value would be what I want (either another
> number or some text). I think it would be enough...
> Subclassing QComboBox, on the other hand, might be interesting if I can
> make this subclass behaves like a combobox with KDevDesigner.
The easiest way would be to simply maintain a separate list:
X = ['a', '','', 'b', 'c','', 'd']
where the position in the list is your index (if the range of your index
values makes that reasonable to do; for example, fill a list with null
strings and then put the values in the positions where they belong).
Then just load/reload the combo box as needed (initially, when the list
changes, etc):
comboBox.clear ()
for item in X:
if item:
comboBox.insertItem (item)
Then all you need to do with the user's selection is:
choiceIndex = X.index (comboBox.currentText ())
and you have its index value from the original list.
That may not be an extremely efficient way to do it if the contents of the
list changes often, or the list is extremely long (in which case, a combo box
probably isn't a good choice anyway).
dicts add the problem of maintaining a sort order if that's important to your
application, but they'd probably be easier if the range of your index values
is large or they make some other part of the app easier to write.
Subclassing would be similar, except the list or dict would be internal to the
QComboBox subclass.
Without knowing the details of the application, it's hard to be more specific.
Jim
More information about the PyQt
mailing list