Trolltech Home | Qt-jambi-interest Home | Recent Threads | All Threads | Author | Date
All threads index page 1

Qt-jambi-interest Archive, October 2006
disposing child widgets


Message 1 in thread

Hi!

I have the following situation: I have a top level window which I close and 
dispose explicitly. It contains a child widget which must react to this 
disposal by cleaning up after itself. But overriding the dispose() method in 
that child widget and reacting to the disposal there does not work because 
that method is never called. I also cannot call the child widget's dispose() 
method explicitly.

The C++ way of doing this would be to put the code in question into the 
destructor. But no such thing exists in Java (the finalize() method does not 
follow the same semantics and in my particular situation its use is out of 
question because it may be called too late)

So what approach should I take to have widgets clean up on their disposal?

Regards,
Gregor


Message 2 in thread

Gregor Mückl wrote:
> Hi!
> 
> I have the following situation: I have a top level window which I close and 
> dispose explicitly. It contains a child widget which must react to this 
> disposal by cleaning up after itself. But overriding the dispose() method in 
> that child widget and reacting to the disposal there does not work because 
> that method is never called. I also cannot call the child widget's dispose() 
> method explicitly.
>
> The C++ way of doing this would be to put the code in question into the 
> destructor. But no such thing exists in Java (the finalize() method does not 
> follow the same semantics and in my particular situation its use is out of 
> question because it may be called too late)
> 
> So what approach should I take to have widgets clean up on their disposal?

Hi Gregor,

I assume you need this because the child widgets are holding certain 
resources, like open socket connections etc...

We have already run into this problem and added a solution for it for 
the next technology preview. There you can override the virtual function 
disposed() which will be called prior to the C++ object being destroyed.

In the mean you will have to manually trigger this behaviour. This can 
be done by doing something like the following prior to disposing your 
toplevel:

List<QObject> list = topLevel.findChildren(WidgetThatNeedsCleanup.class);
for (QObject o : list) {
     ((WidgetThatNeedsCleanup) o).cleanup();
}
topLevel.dispose();

-

best regards,
Gunnar


-
Gunnar