[Interest] What's the recommended way to put QObjects in smart pointers?

Nye kshegunov at gmail.com
Thu May 5 15:28:15 CEST 2016


>
> Well, that's not quite true of course. If you want to show a dialog
> created on the stack, you will have to use it's exec() method. That spins
> an event loop, and that means that a lot can happen in the mean time. It
> can vary from an incoming DBUS messages causing the application to
> terminate to something in response to a signal from that dialog itself, to
> some timer triggering a series of events that lead to the destruction of
> the parent.
>
So, in that sense, Nikos is right.
>
> I prefer just dealing with that using... new and ::open instead.
>

I don't mind your preference, everyone has theirs and I wouldn't presume to
tell people how to write their code. However I'd make two points here:
To delete the dialog's parent you would either call delete directly or use
`deleteLater`. It is true, of course, that QDialog::exec will start a
*local* event loop (do correct me if I'm wrong). However to call delete
directly on the dialog's parent you have to put it somewhere in your code
(presumably in the dialog itself, or in a slot somewhere, which is one of
the exceptions I was referring to: it's not a good idea to delete QObjects
inside slots directly, especially if the said object is the receiver; Qt
doesn't like that at all).

As for the second case where you'd connect to the deleteLater slot, this
wouldn't be an issue, since the deferred delete event is posted in the
*thread's
global* event loop and is not handled by the *local* event loops stack (and
that behavior is documented). So the delete event wouldn't be processed
until the control returns to the thread's event loop, thus the QDialog
would have already been deallocated.

Kind regards,
Konstantin.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20160505/a3cff161/attachment.html>


More information about the Interest mailing list