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

Nikos Chantziaras realnc at gmail.com
Wed May 4 19:56:52 CEST 2016


On 04/05/16 19:09, Nye wrote:
>> [...]
>> Is there a Qt-specific idiom where I can manage QObjects that have a
>> parent with a smart pointer and not have to worry about double-deletion?
>
> The parent will be notified of the child's deletion, so it's generally
> safe to delete the child directly (with a few somewhat rare exceptions).
> So most of the time QScopedPointer or it's STL equivalent work just
> fine. If you need a guarded pointer for when Qt might delete the object
> while you still hold a reference, you can use QPointer. I personally
> detest the unnecessary new, and just create everything with auto storage
> where it's possible. So that's another way you can eliminate new almost
> completely.

 From what I can tell, QPointer does not delete the object, so that's a 
no go.

Putting the objects on the stack doesn't work either, because the Qt 
object model does not support that (it will call 'delete' on the object 
even if it's on the stack.)




More information about the Interest mailing list