[PyKDE] ListViewItems and garbage collection

Neil Stevens neil at qualityassistant.com
Sun Jul 21 22:52:00 BST 2002


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On Sunday July 21, 2002 08:16, Jim Bublitz wrote:
> On 21-Jul-02 Neil Stevens wrote:
> > How do I keep my KListViewItems and instances of classes derived
> > from KListViewItem from being garbage collected?
>
> If you're using a non-NULL parent (KListView or KListViewItem) in
> the constructor, they shouldn't be garbage collected. That assumes
> that there is a chain of ownership above the parent that continues
> to remain in scope and doesn't get garbage collected. If the latter
> isn't true, assigning the KListViewItems to a variable probably
> still won't keep them from getting destroyed when the parent or
> something above it goes out of scope.

Well, my items are being created by the ListView itself, and the ListView 
is passing self as the parent, so there is ownership.

> Can you post a simple test example showing the problem?

Well, I get the expected behavior with QListViewItems, but not with 
KListViewItems or my own ListViewItems that inherit KListViewItem.  The 
idea here is not to load the data for the ListViewItems from my database 
until needed:

class ListViewItem(KListViewItem):
	def __init__(self, number, parent, after = None):
		KListViewItem.__init__(self, parent, after)
		# snip
		# number is the row in the query result to check

	def text(self, col):
		self.fill()
		return KListViewItem.text(self, col)

	def fill(self):
		#snip loading row self.number from parent.currentQuery

class ListView(KListView):
	def __init__(self, parent = None, name = None):
		KListView.__init__(self, parent, name)

	def doQuery(self, db, sql, columns):
		# snip executing the sql query in self.currentQuery

		for i in xrange(self.currentQuery.size()):
			ListViewItem(i, self)

		return 1

When I run this on a query that returns 5 values, I get an empty listview.  
Adding some prints shows that the ListViewItem constructor is called 5 
times but that's it.  paintCell is never called, so they must be being 
destroyed immediately.

If I change the ListViewItem(i, self):

KListViewItem(self, QString.number(i)) gives me no items just the same

QListViewItem(self, QString.number(i)) gives me 5 items displayed, and your 
own example uses QListViewItem instead of KListViewItem.

self.Test = ListViewItem(i, self) gives me the last item

So I'm led to believe that QListViewItem has a special trick in it.

thanks for any help,

- -- 
Neil Stevens - neil at qualityassistant.com
"I always cheer up immensely if an attack is particularly wounding
because I think, well, if they attack one personally, it means they
have not a single political argument left." - Margaret Thatcher
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.0.6 (GNU/Linux)
Comment: For info see http://www.gnupg.org

iD8DBQE9Ox82f7mnligQOmERAt90AJ9KBJfupcb2Yw+Bj04jBMW6zu1A9ACeIEyy
VkLsr6BsvM0nKdmZxR+bENY=
=ITzm
-----END PGP SIGNATURE-----




More information about the PyQt mailing list