fast QListView numerical sort

Uwe Mayer merkosh at
Thu Feb 12 12:15:29 CET 2004


Using PyQt I got a QListView with about 800 entries now (but its intended to
be scalable up to about 3000).
The first column contains numerical data. Now Qt does the sorting all by its
self and quite fast enough. By default it sorts alphanumerically, i.e. 1 10
100 101 110 111 2 ...

Solutions on the web suggested left padding the digits with white spaces
which didn't work for me. 
Left padding with 0's does not look good.
Qt Manual says you can overwrite the compare method -
which I did: = lambda s,a,b,c: s.text(0).toInt()[0]

The compare method returns < 0 for "smaller", 0 for "equal", > 0 for
Doing so works, but causes a 2 sec delay each time a re-sort has to be done
- which is rather annoying (at only less than 1/3 of its expected load!)

Solutions I could come up with are:
a. derive a C++ QListViewItem that does the numerical sorting and use this
instead of the default QListViewItem in the hope it'll be faster
pro: probably the easyest (faster? fast enough?)
con: not portable

b. implement all sorting and inserting related functions by myself in Python
and do the sorting from Python
pro: portable
con: much more coding to be done; probably contains more bugs

Which do you favour or do you have other suggestions?


More information about the Python-list mailing list