Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. About the usage of 'New'

About the usage of 'New'

Scheduled Pinned Locked Moved Unsolved General and Desktop
3 Posts 3 Posters 266 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Sayanel
    wrote on last edited by
    #1

    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

    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by mrjj
      #2

      Hi
      smart pointers are for non Qt classes to handle lifetime.
      Qt comes with a system.
      https://doc.qt.io/qt-5/objecttrees.html
      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.

      1 Reply Last reply
      5
      • Kent-DorfmanK Offline
        Kent-DorfmanK Offline
        Kent-Dorfman
        wrote on last edited by
        #3

        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.

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved