Important: Please read the Qt Code of Conduct -

About the usage of 'New'

  • In most Qt example I find (lets say, this one), an element is added to a widget using a new

    scene = new DiagramScene(itemMenu, this);

    And memory is handled by Qt and ensure that when the parent (this) is destroyed all its children are destroyed as well.
    I have read often that in modern C++, we shall not use 'new', but only smart pointers.

    How are the two compatible? What should I use, and is Qt doc deprecated on this point?

    Thanks, and sorry for rookie question

  • Lifetime Qt Champion

    smart pointers are for non Qt classes to handle lifetime.
    Qt comes with a system.
    and you should not use smart pointers for Qt as it will just crash/wont work.
    besides, it would be completely redundant if you could use. :)
    So nope, it's not deprecated/old. its simply another way than directly smart pointers.

    And to be clear. Using smart pointers, you are still using new. The cleanup is just handled by
    a class. Compared to a raw pointer.

  • kind of alluding to what mrjj wrote: think of heap managed Qt objects as having their own framework specific smart-pointers, without declaring them as such...and do keep them separate from std:: smart pointers.

Log in to reply