[PyKDE] Deleting a layout in PyQt as opposed to Qt...

Giovanni Bajo rasky at develer.com
Wed Feb 28 11:39:49 GMT 2007


On 28/02/2007 12.17, Sundance wrote:

> Am I missing something? Is this an oversight in PyQt, or is there a 
> nice, clean Python equivalent that I've overlooked?

With recent versions of SIP, you can do:

import sip
sip.delete(widget.layout())

=================

Otherwise, if you're bound to an older version, a less clear but effective way 
is to force SIP to transfer ownership of the object back to Python. By doing 
so, you tie the lifetime of the C++ object to the lifetime of the Python object:

import sip
L = widget.layout()
sip.transferback(L)
del L   # drop last Python reference -> destroy C++ object

Or, a more compact but equivalent form:

import sip
sip.transferback(widget.layout())

Of course, for this to work, you need to make sure you don't have *any* other 
Python reference to that layout.

=================

A third solution involves the fact that QLayout is a QObject, so you can use 
its deleteLater() method:

L = widget.layout()
L.deleteLater()
QCoreApplication.sendPostedEvents(L, QEvent.DeferredDelete)
-- 
Giovanni Bajo




More information about the PyQt mailing list