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. Scope and destruction of parent-child model QObjects
Forum Updated to NodeBB v4.3 + New Features

Scope and destruction of parent-child model QObjects

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 4 Posters 1.3k Views 2 Watching
  • 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.
  • T Offline
    T Offline
    TauCeti
    wrote on last edited by
    #1

    Hello,
    I have a question about the scope and the destruction of QT Objects and the parent-child model to manage QObject resources:

    void ClassA::Method1()
    {
       QProgressBar* ProgressBar = new QProgressBar();	
    }
    void ClassA::Method2()
    {
        tableWidget->setItem(0, 0, new QTableWidgetItem(text));
    }
    void ClassA::Method3()
    {
        QStandardItemModel *model = new QStandardItemModel(1, 1,this);
    }
    
    

    It is clear for me, that adding 'this' to the objects like in Method 3 results in:
    when ClassA gets deleted, Object will be deleted too.

    1. What causes the deletion of a QT class (related to parent/child)?
    2. What about Method1 and Method2? Do I have to add a 'this' or
      use delete somewhere?

    Generally, is it more safe to rely on 'this' or should a manual 'delete' be used?

    1. Is std::unique_ptr or std::shared_ptr compatible with OQbjects?

    Thanks for help.

    kshegunovK 1 Reply Last reply
    0
    • VRoninV Offline
      VRoninV Offline
      VRonin
      wrote on last edited by VRonin
      #2

      What causes the deletion of a QT class (related to parent/child)?

      The parent keeps a list of its children. Passing a parent pointer to a QObject constructor or calling QObject::setParent makes the children append itself to the list held by the parent. When the parent destructor is run it goes through the list of children and calls delete on them (QObjectPrivate::deleteChildren)


      What about Method1 and Method2?

      Method 1 is a memory leak, method 2 is not because it's explicitly stated in the docs: "The table takes ownership of the item." meaning QTableWidget::setItem adds the item to the list of children of the QTableWidget.


      Generally, is it more safe to rely on 'this' or should a manual 'delete' be used?

      @VRonin said in Interfaces, abstract, concrete classes and QObject:

      This is the flowchart I normally follow, it covers 99% of the cases:

      • is it a QObject?
        • yes
          • can you give it a parent? (for example moveToThread() prevents the parent-child relationship)
            • yes: use a normal pointer and set the parent
            • no: connect the deleteLater() slot to some signal
        • no
          • can multiple places share ownership of the object or pass ownership from one another? (e.g. in containes)
            • yes: use std::shared_ptr
            • no: use std::unique_ptr

      If you need to use the pointer in a function that should not manage ownership but just access (read/write) the object (this covers 90% of use cases) then use a raw pointer argument (use std::unique_ptr::get() or std::shared_ptr::get() to retrieve the raw pointer from smart pointers)


      Is std::unique_ptr or std::shared_ptr compatible with OQbjects?

      They are, there is nothing fancy about smart pointers

      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
      ~Napoleon Bonaparte

      On a crusade to banish setIndexWidget() from the holy land of Qt

      JKSHJ 1 Reply Last reply
      4
      • VRoninV VRonin

        What causes the deletion of a QT class (related to parent/child)?

        The parent keeps a list of its children. Passing a parent pointer to a QObject constructor or calling QObject::setParent makes the children append itself to the list held by the parent. When the parent destructor is run it goes through the list of children and calls delete on them (QObjectPrivate::deleteChildren)


        What about Method1 and Method2?

        Method 1 is a memory leak, method 2 is not because it's explicitly stated in the docs: "The table takes ownership of the item." meaning QTableWidget::setItem adds the item to the list of children of the QTableWidget.


        Generally, is it more safe to rely on 'this' or should a manual 'delete' be used?

        @VRonin said in Interfaces, abstract, concrete classes and QObject:

        This is the flowchart I normally follow, it covers 99% of the cases:

        • is it a QObject?
          • yes
            • can you give it a parent? (for example moveToThread() prevents the parent-child relationship)
              • yes: use a normal pointer and set the parent
              • no: connect the deleteLater() slot to some signal
          • no
            • can multiple places share ownership of the object or pass ownership from one another? (e.g. in containes)
              • yes: use std::shared_ptr
              • no: use std::unique_ptr

        If you need to use the pointer in a function that should not manage ownership but just access (read/write) the object (this covers 90% of use cases) then use a raw pointer argument (use std::unique_ptr::get() or std::shared_ptr::get() to retrieve the raw pointer from smart pointers)


        Is std::unique_ptr or std::shared_ptr compatible with OQbjects?

        They are, there is nothing fancy about smart pointers

        JKSHJ Offline
        JKSHJ Offline
        JKSH
        Moderators
        wrote on last edited by
        #3

        @VRonin said in Scope and destruction of parent-child model QObjects:

        What causes the deletion of a QT class (related to parent/child)?

        The parent keeps a list of its children. Passing a parent pointer to a QObject constructor or calling QObject::setParent makes the children append itself to the list held by the parent. When the parent destructor is run it goes through the list of children and calls delete on them (QObjectPrivate::deleteChildren)

        Additional reading: https://doc.qt.io/qt-5/objecttrees.html

        Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

        1 Reply Last reply
        2
        • T TauCeti

          Hello,
          I have a question about the scope and the destruction of QT Objects and the parent-child model to manage QObject resources:

          void ClassA::Method1()
          {
             QProgressBar* ProgressBar = new QProgressBar();	
          }
          void ClassA::Method2()
          {
              tableWidget->setItem(0, 0, new QTableWidgetItem(text));
          }
          void ClassA::Method3()
          {
              QStandardItemModel *model = new QStandardItemModel(1, 1,this);
          }
          
          

          It is clear for me, that adding 'this' to the objects like in Method 3 results in:
          when ClassA gets deleted, Object will be deleted too.

          1. What causes the deletion of a QT class (related to parent/child)?
          2. What about Method1 and Method2? Do I have to add a 'this' or
            use delete somewhere?

          Generally, is it more safe to rely on 'this' or should a manual 'delete' be used?

          1. Is std::unique_ptr or std::shared_ptr compatible with OQbjects?

          Thanks for help.

          kshegunovK Offline
          kshegunovK Offline
          kshegunov
          Moderators
          wrote on last edited by
          #4

          @TauCeti said in Scope and destruction of parent-child model QObjects:

          Is std::unique_ptr or std::shared_ptr compatible with OQbjects?

          @VRonin said in Scope and destruction of parent-child model QObjects:

          They are, there is nothing fancy about smart pointers

          Using std::shared_ptr (or QSharedPointer for that matter) with QObjects is good if you enjoy pain often and in copious amounts ...

          Read and abide by the Qt Code of Conduct

          1 Reply Last reply
          1

          • Login

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