[PyKDE] Sort numerical with QListViewItem
Teodor Andersson -
di99ante at chl.chalmers.se
Wed Aug 22 14:48:55 BST 2001
Hi there. This is the first time i port to you and i'm afraid this
question already had been answered. Newbie as I am, I hope y'all have
patience.
I have ripped this code from a post by phil:
<---------------------------------------------------------------------------------------
From: Phil Thompson
Subject: Re: [PyKDE] Help with QListViewItem
Date: Thu, 14 Sep 2000 01:31:08 -0700
Per Gummedal wrote:
>
> I have this simple source.
>
> #!/usr/bin/env python
>
> import string, sys
> from qt import *
>
> class List(QListView):
> def __init__(self, *args):
> apply(QListView.__init__,(self,) + args)
> self.addColumn("A")
> self.addColumn("B")
> self.setShowSortIndicator(1);
> self.data = (['1','2'], ['11','12'],['2','3'])
self.itemList = [] # ADD THIS
> for row in self.data:
> item = ListItem(self)
self.itemList.append(item) # ADD THIS
> for i in range(len(row)):
> item.setText(i, row[i])
>
> class ListItem(QListViewItem):
> def __init__(self, *args):
> apply(QListViewItem.__init__,(self,) + args)
>
> def key(self, col, asc):
> print 'key'
> return string.rjust(str(self.text(col)),2) # CHANGE THIS
>
> a = QApplication(sys.argv)
> mw = List()
> a.setMainWidget(mw)
> mw.show()
> a.exec_loop()
>
> When I click on the columnheaders to sort, method key is not called.
> What am I doing wrong, any ideas ? Make the changes I've identified above. When you call ListItem(self) a
Python ListItem instance is created and a Qt QListViewItem instance is
created. Qt takes responsibility for deleting the QListViewItem
instance. The ListItem instance is deleted as soon as there is no Python
reference to it - in your script the first instance is deleted when you
create the second one, and the second one is deleted when the List
__init__ method terminates. There is therefore no ListItem instance to
provide a key() method. The trick is to keep the ListItem instances
alive by keeping references
to them in the itemList list. Phil
_______________________________________________
PyKDE mailing list PyKDE at mats.gmd.de
http://mats.gmd.de/mailman/listinfo/pykde
<--------------------------------------------------------------------------------------
This kind of sorting is _almost_ what i want. The only difference is
that my list
contains:
> self.data = (['a 1','a 2'], ['a 11','a 12'],['a 2','a 3'])
(it is a cuple of charachters before the number.)
can i sort by numbers anyway?
> return string.rjust(str(self.text(col)),2) # CHANGE THIS
maybe modify the str(self.text(col)) with an regExp or something to
force to sort by numbers?
Best regards,
Teodor
More information about the PyQt
mailing list