[PyKDE] Custom sorting function for the QListView widget

Fernando Jorge Silveira Filho fernando at land.ufrj.br
Sat Jul 3 23:19:00 BST 2004


>>It seems clear to me that there is a performance loss caused by the fact
>>that the compare() method (which is obviously a bottleneck for the
>>sorting routine) is now in the script instead of the binary Qt library.
>>
>
>Hi Fernando,
>
>It's not clear to me, not at least without seeing your code.  Can you post it, 
>or post some digest of it?  
>
>
Hi Troy,

Below is a simplified code that suffers from the same problem. On the 
other side, I agree that I may be wrong about blaming the interface 
between C++ and Python for degrading the performance in this particular 
case. However, this is just what my intuition (which is wrong no less 
than often :-)) tells me. It seems to me that if the Qt library had 
support for numerical key comparisions within itself the whole process 
would be faster without so many changes between C++ and Python code.

>>Since customized sorting for ListViews seems like such a common task for
>>me, I am wondering whether I did something wrong, or if there is another
>>way of doing this within PyQt.
>>
>
>Have you tried re-implementing QListViewItem.key() ?
>
>
>
No I did not try that yet. Anyway, it seems that I would not be able to 
get much from that since I would still be bound to the return type of 
QListViewItem.key() which is QString.


Now for the code:


BEGIN_OF_CODE

from qt import *
import sys

# QListViewItem Subclass
class MyQListViewItem(QListViewItem):
    # C++ Prototype
    # int MyListViewItem::compare( QListViewItem *i, int col,
    #                             bool ascending ) const
    def compare( self, i, col, ascending ):
        x = self.key( col, ascending ).toInt( );
        y = i.key( col, ascending).toInt( );
        return x[0]-y[0];

# Main program

app=QApplication(sys.argv)

lst  = QListView();
lst.addColumn("Count");

for i in range(1000):
    item = MyQListViewItem(lst, "%s" % i)

app.setMainWidget(lst)
lst.show()
app.exec_loop()

END_OF_CODE




More information about the PyQt mailing list