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. Why Parent class in constructor?
Forum Update on Monday, May 27th 2025

Why Parent class in constructor?

Scheduled Pinned Locked Moved Unsolved General and Desktop
3 Posts 3 Posters 767 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.
  • P Offline
    P Offline
    pk23081996
    wrote on 22 Jun 2020, 05:57 last edited by VRonin
    #1

    I was strolling through the Qt examples and saw this:

    class MainWidget : public QWidget
    {
        Q_OBJECT
    public:
        explicit MainWidget(QWidget *parent = 0);
        ....
    }
    

    I was curious to know why do we need to pass a parent object to the constructor? Is this only to maintain a parent-child relationship or does it serve some another purpose as well?

    And what does this statement do? : 
    MainWidget::MainWidget(QWidget *parent) :
                        **QWidget(parent)**
    { }
    

    We initialize a QWidget object with the parent here but what does it actually do?
    Thanks

    J P 2 Replies Last reply 22 Jun 2020, 06:00
    0
    • P pk23081996
      22 Jun 2020, 05:57

      I was strolling through the Qt examples and saw this:

      class MainWidget : public QWidget
      {
          Q_OBJECT
      public:
          explicit MainWidget(QWidget *parent = 0);
          ....
      }
      

      I was curious to know why do we need to pass a parent object to the constructor? Is this only to maintain a parent-child relationship or does it serve some another purpose as well?

      And what does this statement do? : 
      MainWidget::MainWidget(QWidget *parent) :
                          **QWidget(parent)**
      { }
      

      We initialize a QWidget object with the parent here but what does it actually do?
      Thanks

      J Offline
      J Offline
      jsulm
      Lifetime Qt Champion
      wrote on 22 Jun 2020, 06:00 last edited by
      #2

      @pk23081996 said in Why Parent class in constructor?:

      Is this only to maintain a parent-child relationship

      Yes, parent/child.
      What parent means for QWidget based classes (in addition to parent/child based memory management) can be found here: https://doc.qt.io/qt-5/qwidget.html#QWidget

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      2
      • P pk23081996
        22 Jun 2020, 05:57

        I was strolling through the Qt examples and saw this:

        class MainWidget : public QWidget
        {
            Q_OBJECT
        public:
            explicit MainWidget(QWidget *parent = 0);
            ....
        }
        

        I was curious to know why do we need to pass a parent object to the constructor? Is this only to maintain a parent-child relationship or does it serve some another purpose as well?

        And what does this statement do? : 
        MainWidget::MainWidget(QWidget *parent) :
                            **QWidget(parent)**
        { }
        

        We initialize a QWidget object with the parent here but what does it actually do?
        Thanks

        P Offline
        P Offline
        Pablo J. Rogina
        wrote on 22 Jun 2020, 13:52 last edited by
        #3

        @pk23081996 said in Why Parent class in constructor?:

        We initialize a QWidget object with the parent here but what does it actually do?

        Look at the documentation about object trees & ownership. One main benefit is:

        When you create a QObject with another object as parent, it's added to the parent's children() list, and is deleted when the parent is

        Upvote the answer(s) that helped you solve the issue
        Use "Topic Tools" button to mark your post as Solved
        Add screenshots via postimage.org
        Don't ask support requests via chat/PM. Please use the forum so others can benefit from the solution in the future

        1 Reply Last reply
        1

        1/3

        22 Jun 2020, 05:57

        • Login

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