From e869f7124f0e13bea7f35d5f5a91bc89dc1dcd4e Mon Sep 17 00:00:00 2001 From: Chris Colbert Date: Fri, 16 Nov 2012 18:39:03 -0500 Subject: [PATCH] Dispatch the child added/removed immediately when possible. Dispatch the destroy action to a later time if the object is not yet initialized. --- enaml/qt/qt_object.py | 26 ++++++++++++++++++-------- 1 file changed, 18 insertions(+), 8 deletions(-) diff --git a/enaml/qt/qt_object.py b/enaml/qt/qt_object.py index 51b57fbb..8d34488c 100644 --- a/enaml/qt/qt_object.py +++ b/enaml/qt/qt_object.py @@ -451,11 +451,12 @@ def set_parent(self, parent): The parent of this object, or None if it has no parent. """ - # Note: The added/removed events must be executed on the next - # cycle of the event loop. It's possible that this method is - # being called from the `construct` class method and the child - # of the widget will not yet exist. This means that child event - # handlers that rely on the child widget existing will fail. + # Note: If this object is not yet fully intialized, then the + # added/removed events must be executed on the next cycle of + # the event loop. It's possible that this method is being called + # from the `construct` class method and the toolkit widget will + # not yet exist. This means that child event handlers that rely + # on the child toolkit widget existing will fail. curr = self._parent if curr is parent or parent is self: return @@ -465,12 +466,18 @@ def set_parent(self, parent): if self in curr._children: curr._children.remove(self) if curr._initialized: - QtObject.deferred_call(curr.child_removed, self) + if self._initialized: + curr.child_removed(self) + else: + QtObject.deferred_call(curr.child_removed, self) if parent is not None: parent._children.append(self) if parent._initialized: - QtObject.deferred_call(parent.child_added, self) + if self._initialized: + parent.child_added(self) + else: + QtObject.deferred_call(parent.child_added, self) def child_removed(self, child): """ Called when a child is removed from this object. @@ -628,5 +635,8 @@ def on_action_destroy(self, content): This method will call the `destroy` method on the object. """ - self.destroy() + if self._initialized: + self.destroy() + else: + QtObject.deferred_call(self.destroy)