[prev in list] [next in list] [prev in thread] [next in thread] 

List:       pykde
Subject:    [PyKDE] Sort numerical with QListViewItem
From:       Teodor Andersson - <di99ante () chl ! chalmers ! se>
Date:       2001-08-22 14:48:55
[Download RAW message or body]

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


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic