[PyKDE] Re: Using comboboxes indexes for referencing data

Jorge Godoy godoy at ieee.org
Wed May 11 00:09:52 BST 2005


Jim Bublitz <jbublitz at nwinternet.com> writes:

> 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.

This would add blanks in the middle of the combobox items.  It isn't that
elegant, specially if something has index 0 and another thing has index 50 or
1000.  I could eliminate those blank spaces, when inserting, but it looks like
something that isn't all that clean. 

> 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).

It is acceptable in this case, but definitely it won't last forever. :-)
There are, on the other hand, other restrictions such as screen size and the
amount of information I need to display and this seemed to be the best option
to show everything.

> 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.

I can ask Qt to sort items for me in some particular way and that will do it.
It doesn't matter to me the order of insertion, alphabetical order makes more
sense in 99% of the cases, so if I need something different I can do the work
by hand in that 1%...

> Subclassing would be similar, except the list or dict would be internal to the 
> QComboBox subclass.

This is what I was thinking.  Then I'd have to handle some changes on
QSqlPropertyMap as well to get the right property in an automatic way...  

I'll have to take a look at this.

> Without knowing the details of the application, it's hard to be more specific.

Hey!  Leave some of the fun to me. :-)  I just want some hints, not that you
do the work for me :-)  I'm still learning and seeing how experienced people
do that is, IMHO, the best way.

Thank you very much for your help!  I'll see what I can get from here.

-- 
Jorge Godoy      <godoy at ieee.org>




More information about the PyQt mailing list