[PyKDE] Destroying pyqt child widgets

Brent Burley Brent.Burley at disney.com
Fri Nov 16 19:12:55 GMT 2001


How does one destroy a child widget in pyqt?  If you completely deref a
top level widget, it and all its children will be destroyed.  But what
if you only want to destroy the child and not its parent?

In C++ you can just say "delete child;" and qt will destroy it and
remove it from its parent.  But there's no equivalent in pyqt.

In python you can do "child.destroy()" but this only frees with window
system resources and leaves the child widget there.  This is bad and can
make Qt do some pretty strange things.  In fact, QWidget::destroy is
protected in C++ and shouldn't even be callable; it's normally only
called by the QWidget destructor.

I've come up with this workaround:

  def deleteQWidget(w):
    w.reparent(QWidget(), 0, QPoint())

It's hacky, but it does the job.  The widget is transferred to a
temporary parent and then both are immediately destroyed.

I've already asked Phil if he could expose a way to call the C++ delete
operator on a PyQt object.  But I thought that others should be aware of
the issue in the meantime.

I've also asked Phil if we could have a __cppdel__ method that gets
called by sip when the underlying C++ object is being destroyed, but I
haven't heard back from him yet.

	Brent Burley




More information about the PyQt mailing list