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. QObject member variable destruction: deleteLater vs this
Forum Updated to NodeBB v4.3 + New Features

QObject member variable destruction: deleteLater vs this

Scheduled Pinned Locked Moved Unsolved General and Desktop
3 Posts 3 Posters 218 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

    What is a better way to handle the destruction of a QObject class member variable?
    With a destructor (class A) or with a "this" and therefore rely on the parent destruction (class B)?

    class A{
    public:
    ~A() {
     m_label->deleteLater();
    }
    
    void function(){
     m_label = new QLabel;
    }
    private:
     QLabel* m_label;
    }
    
    class B{
    public:
    
    void function(){
     m_label = new QLabel(this);
    }
    private:
     QLabel* m_label;
    }
    

    Thanks for help.

    1 Reply Last reply
    0
    • fcarneyF Offline
      fcarneyF Offline
      fcarney
      wrote on last edited by
      #2

      "this" is cleaner I think. It also lets you have the class system manage the memory as intended. It also does not keep you from deleting it earlier either. I really don't see downsides to "this". With deleteLater you will be riddling your code with calls.

      C++ is a perfectly valid school of magic.

      1 Reply Last reply
      1
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #3

        Hi,

        One thing that your code does not show is what are the base classes of class A and B. If you want to use them as parent of the members variable, they have to from a suitable base classe.

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/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