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. What is the difference between specifying a Widget parent with the Class and with the constructor?
Forum Updated to NodeBB v4.3 + New Features

What is the difference between specifying a Widget parent with the Class and with the constructor?

Scheduled Pinned Locked Moved Unsolved General and Desktop
10 Posts 3 Posters 3.6k 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.
  • D Offline
    D Offline
    DevinQT
    wrote on last edited by
    #1

    Hey,

    You have two ways you can specify a parent. After public in the Class declaration:
    class Widget : public QWidget

    And through the constructor:
    RockWidget::RockWidget(QWidget* parent) : QWidget(parent)

    What is the difference?
    (p.s. I am new to this..)

    1 Reply Last reply
    0
    • dheerendraD Offline
      dheerendraD Offline
      dheerendra
      Qt Champions 2022
      wrote on last edited by dheerendra
      #2

      No two ways specifying the parent. Both are two different things.

      1. In case of declaration it is not parent. QWidget is base class of RockWidget. It is class relationship.
      2. In the case of constructor, QWidget type object is passed as parent to object which is constructed from RockWidget. Here it is object->object relationship

      Dheerendra
      @Community Service
      Certified Qt Specialist
      http://www.pthinks.com

      1 Reply Last reply
      7
      • D Offline
        D Offline
        DevinQT
        wrote on last edited by DevinQT
        #3

        Oh that's nice.

        Does an object's parent always have to be another object of the same class as the child's object class has as a class-parent?

        And: When the constructor says QWidget(parent) in the initializer list (and basically says QWidget = parent, right? (Parent being input from us)), what does that statement make a parent or how does that statement make or create a parent?
        I am trying to find out if this is some Qt specific behavior, a Qt construct, or that this is just C++. Is there something in the keyword parent? Or is that just a name and is just saying QWidget = something, enough to make a parent? (If the constructor argument was also 'something'.

        jsulmJ 1 Reply Last reply
        0
        • D DevinQT

          Oh that's nice.

          Does an object's parent always have to be another object of the same class as the child's object class has as a class-parent?

          And: When the constructor says QWidget(parent) in the initializer list (and basically says QWidget = parent, right? (Parent being input from us)), what does that statement make a parent or how does that statement make or create a parent?
          I am trying to find out if this is some Qt specific behavior, a Qt construct, or that this is just C++. Is there something in the keyword parent? Or is that just a name and is just saying QWidget = something, enough to make a parent? (If the constructor argument was also 'something'.

          jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @DevinQT said in What is the difference between specifying a Widget parent with the Class and with the constructor?:

          Does an object's parent always have to be another object of the same class as the child's object class has as a class-parent?

          No, it must be the same type as specified for the parent (or a derived class):

          RockWidget::RockWidget(QWidget* parent) : QWidget(parent)
          

          In this case parent must be a QWidget or a class derived from QWidget.

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

          1 Reply Last reply
          4
          • dheerendraD Offline
            dheerendraD Offline
            dheerendra
            Qt Champions 2022
            wrote on last edited by
            #5

            In addition to what @jsulm said please note. What ever you do in qt is all c++. Parent is just a variable name. It can be 'something' as well. It is just a object relationship ship.

            Dheerendra
            @Community Service
            Certified Qt Specialist
            http://www.pthinks.com

            1 Reply Last reply
            2
            • D Offline
              D Offline
              DevinQT
              wrote on last edited by
              #6

              So "RandomClassName* Name = RandomObjectOfThatClass" in the constructor of an object makes that object the child of the object specified in the constructor in every C++ program?

              jsulmJ 1 Reply Last reply
              0
              • dheerendraD Offline
                dheerendraD Offline
                dheerendra
                Qt Champions 2022
                wrote on last edited by
                #7

                C++ does not have any class like that. If you write class like the way specified it will work.

                Dheerendra
                @Community Service
                Certified Qt Specialist
                http://www.pthinks.com

                1 Reply Last reply
                1
                • D DevinQT

                  So "RandomClassName* Name = RandomObjectOfThatClass" in the constructor of an object makes that object the child of the object specified in the constructor in every C++ program?

                  jsulmJ Offline
                  jsulmJ Offline
                  jsulm
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  @DevinQT No. Parent - child relationship is a Qt thing, it is not C++ specific.
                  Also

                  RandomClassName* Name = RandomObjectOfThatClass
                  

                  is not really valid C++ code, I'm not sure what you want to ask here. And if RandomObjectOfThatClass is a pointer to a RandomClassName instance you would simply declare a local pointer to that instance with this line of code (though ; is missing).
                  This is how a parent is specified:

                  RockWidget::RockWidget(QWidget* parent) : QWidget(parent)
                  {
                  ...
                  }
                  

                  So, you pass a pointer to the parent as parameter to the child. The pointer can be nullptr, in this case there is no parent - child relationship.
                  You should read http://doc.qt.io/qt-5/objecttrees.html

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

                  1 Reply Last reply
                  1
                  • D Offline
                    D Offline
                    DevinQT
                    wrote on last edited by DevinQT
                    #9

                    Sorry with 'RandomObjectOfThatClass' i meant that that is what's basically happening in the constructor when it's called... So it is the argument of the new object.

                    RockWidget* NewRW = new RockWidget(RandomObjectOfThatClass)
                    

                    And the constructor would be the same as you point out. So i supplanted 'parent' with Name, and QWidget with RandomClassName.

                    RockWidget::RockWidget(RandomClassName* Name) : RandomClassName(Name)
                    

                    So that was how i meant my previous comment.

                    My question is; I think it's just not clear to me why setting an object in the constructor to be equal to another existing object makes a parent.. The same way that setting an (w) integer in the the constructor to set the existing width variable makes width not a parent. Is it just with objects that this is happening? (I know integers couldn't be something like a parent, but i hope you get my point) Or is there is some magic happening here? How does Qt know that with 'QWidget = parent' i mean to create a parent? I should note that i haven't seen this concept object-to-object relationships in normal C++ tutorials yet.

                    _

                    @jsulm said in What is the difference between specifying a Widget parent with the Class and with the constructor?:

                    No, it must be the same type as specified for the parent (or a derived class):

                    With specified you mean specified in the constructor? or in the class? (The "class RockWidget : public QWidget"?) Are they related? If i try to make an 'OwnClass' a parent i get an error:

                    0_1548685120557_Screenshot 2019-01-28 at 15.16.37.png

                    jsulmJ 1 Reply Last reply
                    0
                    • D DevinQT

                      Sorry with 'RandomObjectOfThatClass' i meant that that is what's basically happening in the constructor when it's called... So it is the argument of the new object.

                      RockWidget* NewRW = new RockWidget(RandomObjectOfThatClass)
                      

                      And the constructor would be the same as you point out. So i supplanted 'parent' with Name, and QWidget with RandomClassName.

                      RockWidget::RockWidget(RandomClassName* Name) : RandomClassName(Name)
                      

                      So that was how i meant my previous comment.

                      My question is; I think it's just not clear to me why setting an object in the constructor to be equal to another existing object makes a parent.. The same way that setting an (w) integer in the the constructor to set the existing width variable makes width not a parent. Is it just with objects that this is happening? (I know integers couldn't be something like a parent, but i hope you get my point) Or is there is some magic happening here? How does Qt know that with 'QWidget = parent' i mean to create a parent? I should note that i haven't seen this concept object-to-object relationships in normal C++ tutorials yet.

                      _

                      @jsulm said in What is the difference between specifying a Widget parent with the Class and with the constructor?:

                      No, it must be the same type as specified for the parent (or a derived class):

                      With specified you mean specified in the constructor? or in the class? (The "class RockWidget : public QWidget"?) Are they related? If i try to make an 'OwnClass' a parent i get an error:

                      0_1548685120557_Screenshot 2019-01-28 at 15.16.37.png

                      jsulmJ Offline
                      jsulmJ Offline
                      jsulm
                      Lifetime Qt Champion
                      wrote on last edited by
                      #10

                      @DevinQT said in What is the difference between specifying a Widget parent with the Class and with the constructor?:

                      With specified you mean specified in the constructor?
                      RockWidget is derived from QWidget, right?
                      QWidget is derived from QObject and hence implements parent/child relationship.
                      Now, if you go to http://doc.qt.io/qt-5/qwidget.html#QWidget you will see that parent must be a QWidget:

                      QWidget::QWidget(QWidget *parent = nullptr, Qt::WindowFlags f = ...)
                      

                      "If i try to make an 'OwnClass' a parent i get an error" - yes, because RockWidget is not derived from OwnClass but from QWidget..

                      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