473,396 Members | 2,059 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

fast QListView numerical sort

Hi,

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 QListViewItem.compare -
which I did:

QListViewItem.compare = lambda s,a,b,c: s.text(0).toInt()[0]
-a.text(0).toInt()[0]

The compare method returns < 0 for "smaller", 0 for "equal", > 0 for
"bigger".
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?

Thanks
Uwe
Jul 18 '05 #1
2 3002

"Uwe Mayer" <me*****@hadiko.de> wrote in message
news:c0**********@news.rz.uni-karlsruhe.de...
Hi,

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.
Hmm, I'd expect it to work.
Left padding with 0's does not look good.
Qt Manual says you can overwrite the compare method QListViewItem.compare - which I did:

QListViewItem.compare = lambda s,a,b,c: s.text(0).toInt()[0]
-a.text(0).toInt()[0]

The compare method returns < 0 for "smaller", 0 for "equal", > 0 for
"bigger".
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!)
Well, this probably comes from PyQt's nature - its a layer between Qt
(which is C++)
and Python. QListView calls some C++ function to sort its items, which in
turn makes
a lot of calls to QListViewItem::compare(). Every call to compare() has to
be
translated from C++ world to Python world, and the result returned back.
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
That should be fast enough, I guess, this is the ultimate speed you can
get.
Why isn't it 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
I'm not at all sure that it will be faster than what you currently have.
It sure might be
because you'd remove most of that translation layer calls.
Which do you favour or do you have other suggestions?

I'd say check that space-filled approach. It should work one way or
another.

Cheers,
Andy.

Jul 18 '05 #2
Andy Salnikov wrote:
The compare method returns < 0 for "smaller", 0 for "equal", > 0 for
"bigger".
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!) Well, this probably comes from PyQt's nature - its a layer between Qt
(which is C++)
and Python. QListView calls some C++ function to sort its items, which in
turn makes
a lot of calls to QListViewItem::compare(). Every call to compare() has to
be
translated from C++ world to Python world, and the result returned back.
Yes. I found another suggestion on the web: overwriting the .key() method,
which has to return the value that is to be compared during sorting.
The .key() method returns a left zero-padded string and thus the string
sorting will yield the correct results.
However, this is even slower than overwriting the .compare() method.
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

That should be fast enough, I guess, this is the ultimate speed you can
get.
Why isn't it portable?


.... yeah, it surely will run on Windows, but then I'll have to take care of
the cross-compilation, etc. Which Windows user has a C++ compiler
installed?
Portability is surely my (personally) least problem, but working with Python
its not nice giving up this "portability" feature: take the sources to
another platform - run.

Another possibility: Have you got any experience with Psyco? I just heard
about it, but didn't go into much details until now. Perhaps the
just-in-time compiler will boost the performance...

Thanks
Uwe
Jul 18 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Bob Parnes | last post by:
I cannot find a reference on identifying multiple selections in QListView. Apparently one has to iterate through all the items and test each individually, but I don't know how to do the iteration....
3
by: Ali Chambers | last post by:
Hi, I have a bit of a problem with a sort procedure I need to do. I have a list of items in a listbox, eg:- 2.3%<A other text here> -4%<B other text here> 10%<C other text here> -9.3%<D...
2
by: Josema | last post by:
Hi to all, I have a datagrid filled with string values, and when i click in a header column the datagrid is alphanumerical sorted , but i need sort some columns by numeric way... The...
7
by: Flavio | last post by:
Hi, I have a QListview widget that allows me to store a bunch of strings in it. This strings can be visualized, sorted, selected, etc. My Problem is that I cant find a way to get the user...
2
by: Svenn Bjerkem | last post by:
Hi, I am looking for a bit more elaboration on the problem of deleting elements from a QListsView. I know this is a tricky problem with references, but I have not been able to extract enough...
23
by: Abhi | last post by:
Hi.. I wanted the C source code in machine readable format for the book "Numerical Recipes in C". I got hold of the pdf version of the book somehow. Does anyone have the complete C code of the...
2
by: jediknight | last post by:
Hi, I have a listview which has columns of text and columns of numerical data. I need to be able to sort these columns into ascending/desending order whenever the user clicks on the column...
0
by: Vojta Drbohlav | last post by:
Hello, Can I get all text from first column in QListView? How? PS. Sorry for my bad English ;) Than you, Vojta
1
by: Reinaldo Carvalho | last post by:
Hi, I programming with qt module and i have a qWidgetTab with a qListView inside, and i need update the qListView every 5 seconds, how do this on transparent mode to user. I do a function to...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.