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 Updated to NodeBB v4.3 + New Features

Why Parent class in constructor?

Scheduled Pinned Locked Moved Unsolved General and Desktop
3 Posts 3 Posters 769 Views 1 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.
  • P Offline
    P Offline
    pk23081996
    wrote on 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

    jsulmJ Pablo J. RoginaP 2 Replies Last reply
    0
    • P pk23081996

      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

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on 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

        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

        Pablo J. RoginaP Offline
        Pablo J. RoginaP Offline
        Pablo J. Rogina
        wrote on 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

        • Login

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