[PyKDE] Help with QListViewItem

Phil Thompson phil at river-bank.demon.co.uk
Thu Sep 14 10:36:30 BST 2000


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




More information about the PyQt mailing list