[PyKDE] removeChild cannot be used to destroy QWidgets

David K. Hess dhess at verscend.com
Mon Aug 29 00:03:53 BST 2005


After debugging some segfaults, researching past posts on the list  
and reading Phil's FAQ entry on the topic, I wanted to contibute what  
I've discovered about this: using removeChild() to delete a widget is  
*absolutely the wrong way to delete a widget*.

I have an app that is very dynamic. Widgets are getting added and  
deleted on a regular basis. I was using removeChild() as the means of  
deleting widgets that need to be removed. As things got more  
complicated, I started having segfaults in the focusNextPrevChild()  
method of my main window when it would gain focus. I couldn't predict  
when they would happen, but they would happen regularly.

As I looked at the Qt source and did more debugging it became clear  
that the tab focus ring for the main window was getting corrupted.  
The way this appears to be managed is that when widgets get added as  
children to another widget they add themselves to the tab focus ring  
of the new parent widget's top level widget. However, they are only  
properly removed from the tab focus ring in their destructor, and  
only if they are still parented to the top level widget whose tab  
focus ring references them.

So in other words, removeChild() as a widget destruction method is  
guaranteed to leave the tab focus ring in the top level widget with  
references to deleted widgets and will likely result in a segfault at  
some point. reparent() or deleteLater() will properly disentangle the  
widget from its top level widget's tab focus ring before deletion.

Dave




More information about the PyQt mailing list