[Qt-interest] QObject::deleteLater and private event loop (not using QCoreApplication::exec())

Bo Thorsen bo at fioniasoftware.dk
Fri Jun 10 15:22:16 CEST 2011


Den 10-06-2011 14:19, Nikos Chantziaras skrev:
>> Not exactly. The point of deleteLater is that you know the object will
>> >  be deleted some time after the function returned - how long exactly is
>> >  irrelevant.
> It doesn't look very irrelevant to me though, simply because of the
> nature of the word "irrelevant."  Meaning that once you call
> deleteLater(), you shouldn't call anything else on that object since you
> don't know when it's going to be deleted.

My point with the "irrelevant" is that you know it will happen at some 
point in the event loop, but not exactly when. You use this when you 
want to continue using the object for the rest of the function.

In some cases this is necessary. Your application will crash if you call 
"delete this" in a slot that was called from a signal-slot connection.

>> >  But with any kind of QEventLoop->exec (for example in an error message
>> >  some other place in the code), your knowledge wouldn't hold, unless this
>> >  rule was in place.
> Then why call deleteLater() in the first place?  Only call it when
> you're done with the object.

I use deleteLater for two things: Connecting signals to it (like 
finished()) and as a substitute for "delete this" in slots.

But I also use it to substitute this kind of code:

const bool shouldDelete = something();
... // code that uses this object
if (shouldDelete)
   delete this;

with

deleteLater();
... // code that uses this object

It's a nice tool for what it does. I could certainly develop without it, 
but why would I?

Bo Thorsen,
Fionia Software.

-- 

Expert Qt and C++ developer for hire
Contact me if you need expert Qt help
http://www.fioniasoftware.dk



More information about the Qt-interest-old mailing list