What is the difference between specifying a Widget parent with the Class and with the constructor?
-
-
No two ways specifying the parent. Both are two different things.
- In case of declaration it is not parent. QWidget is base class of RockWidget. It is class relationship.
- 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
-
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'. -
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'.@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.
-
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.
-
C++ does not have any class like that. If you write class like the way specified it will work.
-
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?
@DevinQT No. Parent - child relationship is a Qt thing, it is not C++ specific.
AlsoRandomClassName* Name = RandomObjectOfThatClassis 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 -
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:

-
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:

@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..