Confusion between the usage of 'this' and 'parent'
-
@Swati777999 said in Confusion between the usage of 'this' and 'parent':
In the above code, in place of 'this' when I write parent , my program crashes.
Your code crashes because
parent
wasnullptr
. You must not operate on null pointers.layout w;
The line of code above is the same as
layout w(nullptr);
Can you tell me why?ohh , I see, you're correct , in the constructor the parent has been set to null pointer but is it not the standard structure of any Qt project?
-
ohh , I see, you're correct , in the constructor the parent has been set to null pointer but is it not the standard structure of any Qt project?
@Swati777999 said in Confusion between the usage of 'this' and 'parent':
but is it not the standard structure of any Qt project?
parent can be nullptr if the object does not have a parent.
So, you should always check the parent pointer before using it!if (parent) { parent->... }
-
@Rozhin_so
I am acquainted with the concept of inheritance of C++ but in this case, I am more concerned about accessing the parent object in child class via parent or this keyword.@Swati777999 said in Confusion between the usage of 'this' and 'parent':
I am more concerned about accessing the parent object in child class via parent
Then how can I add widgets to the mainwindow
You are asking/worrying about the wrong thing! Please re-read the replies from @jsulm above and think about them
But why would you want to do so?
Child should not change its parent, else it is bad design.
And again: you should NOT manipulate parent in child! It is really bad design.
Why are you trying to access the
parent
in the child at all? 99% of the time children should never need to look at/accessparent
, and you will be in that 99%! The correct place to manipulate things inside the parent/main window is inside that parent/main window, and not in any child! -
@Swati777999 said in Confusion between the usage of 'this' and 'parent':
In the above code, in place of 'this' when I write parent , my program crashes.
Your code crashes because
parent
wasnullptr
. You must not operate on null pointers.layout w;
The line of code above is the same as
layout w(nullptr);
Can you tell me why?Now, I am somehow able to connect the dots and understand the connectivity well.
In the constructor implementation [layout.cpp] , *parent points to the null pointer [ done in its declaration] intentionally so that we can independently create objects of parent and child in layout.cpp for simplicity.
-
Now, I am somehow able to connect the dots and understand the connectivity well.
In the constructor implementation [layout.cpp] , *parent points to the null pointer [ done in its declaration] intentionally so that we can independently create objects of parent and child in layout.cpp for simplicity.
@Swati777999 said in Confusion between the usage of 'this' and 'parent':
intentionally so that we can independently create objects of parent and child in layout.cpp for simplicity.
No.
parent pointer can be nullptr simply because not every object has a parent.
I really hope you will spend some minutes to read the documentation I gave you... -
@Swati777999 said in Confusion between the usage of 'this' and 'parent':
accessing the parent object in child class via parent or this keyword
Via "this" you access your object, not parent.
And again: you should NOT manipulate parent in child! It is really bad design.
If you want to know what parent is used for please read https://doc.qt.io/qt-5/objecttrees.html@jsulm said in Confusion between the usage of 'this' and 'parent':
@Swati777999 said in Confusion between the usage of 'this' and 'parent':
accessing the parent object in child class via parent or this keyword
Via "this" you access your object, not parent.
And again: you should NOT manipulate parent in child! It is really bad design.
If you want to know what parent is used for please read https://doc.qt.io/qt-5/objecttrees.htmlYes, I read this document but when it comes to implementing concepts that I read, I make a lot of blunders(please bear with me ) and red underlines are enough to scare off a beginner like me.
The sequence of my non-instantaneous reply in this forum is adding woes to the confusion. I can't post a reply within 10 minutes of my last reply and by the time, I am eligible to post my next reply, I had already received responses by other members. It's lil frustrating for not being able to reply immediately.
-
@jsulm said in Confusion between the usage of 'this' and 'parent':
@Swati777999 said in Confusion between the usage of 'this' and 'parent':
accessing the parent object in child class via parent or this keyword
Via "this" you access your object, not parent.
And again: you should NOT manipulate parent in child! It is really bad design.
If you want to know what parent is used for please read https://doc.qt.io/qt-5/objecttrees.htmlYes, I read this document but when it comes to implementing concepts that I read, I make a lot of blunders(please bear with me ) and red underlines are enough to scare off a beginner like me.
The sequence of my non-instantaneous reply in this forum is adding woes to the confusion. I can't post a reply within 10 minutes of my last reply and by the time, I am eligible to post my next reply, I had already received responses by other members. It's lil frustrating for not being able to reply immediately.
@Swati777999 I upvoted your last post. Now you should be able to answer faster :-)
-
@Swati777999 I upvoted your last post. Now you should be able to answer faster :-)
@jsulm
So kind of you! :) -
ohh , I see, you're correct , in the constructor the parent has been set to null pointer but is it not the standard structure of any Qt project?
@Swati777999 said in Confusion between the usage of 'this' and 'parent':
is it not the standard structure of any Qt project?
A widget can have 0 or 1 parent, and it can have many children. This is what https://doc.qt.io/qt-5/objecttrees.html says.
In your code,
w
is the parent ofquit
. In other words,quit
is a child ofw
.Tell me: Who is the parent of
w
? -
Now, I am somehow able to connect the dots and understand the connectivity well.
In the constructor implementation [layout.cpp] , *parent points to the null pointer [ done in its declaration] intentionally so that we can independently create objects of parent and child in layout.cpp for simplicity.
@Swati777999 said in Confusion between the usage of 'this' and 'parent':
*parent points to the null pointer [ done in its declaration] intentionally
This is wrong. The declaration contains the default value. Please see https://www.programiz.com/cpp-programming/default-argument
Compare the following constructor declarations:
// layout.h layout(QWidget *parent = nullptr) // qpushbutton.h QPushButton(const QString &text, QWidget *parent = nullptr)
@JonB said in Confusion between the usage of 'this' and 'parent':
Why are you trying to access the
parent
in the child at all?That's not the intention, I believe. OP just has a big misunderstanding that "
w
is the parent object in this code, therefore the pointerparent
should point tow
.@Swati777999, note that in your code
parent->setLayout(gridLay);
is the same asthis->parentWidget()->setLayout(gridLay);
-
@Swati777999 said in Confusion between the usage of 'this' and 'parent':
is it not the standard structure of any Qt project?
A widget can have 0 or 1 parent, and it can have many children. This is what https://doc.qt.io/qt-5/objecttrees.html says.
In your code,
w
is the parent ofquit
. In other words,quit
is a child ofw
.Tell me: Who is the parent of
w
?Tell me: Who is the parent of
w
?I think w is the primary parent of this parent-child-ladder. It's from where this hierarchy of parent-child originates, I think.
-
@Swati777999 said in Confusion between the usage of 'this' and 'parent':
*parent points to the null pointer [ done in its declaration] intentionally
This is wrong. The declaration contains the default value. Please see https://www.programiz.com/cpp-programming/default-argument
Compare the following constructor declarations:
// layout.h layout(QWidget *parent = nullptr) // qpushbutton.h QPushButton(const QString &text, QWidget *parent = nullptr)
@JonB said in Confusion between the usage of 'this' and 'parent':
Why are you trying to access the
parent
in the child at all?That's not the intention, I believe. OP just has a big misunderstanding that "
w
is the parent object in this code, therefore the pointerparent
should point tow
.@Swati777999, note that in your code
parent->setLayout(gridLay);
is the same asthis->parentWidget()->setLayout(gridLay);
On running the above code! Layout_Example.PNG, I get the following result.
This is the result that I'm getting because of the code that I wrote in main.cpp .
The codes are written in layoutExample.cpp , is not making any change in the main window, even if I write like below..
LayoutExample::LayoutExample(QWidget *parent) : QMainWindow(parent) { box = new QWidget(parent); button1=new QPushButton("Click Here",parent); button2 = new QPushButton("Sign up",parent); gridLay = new QGridLayout(parent); box->setFixedSize(100,100); ................. ................. ...............
All I want to know, is how can I add widgets in this mainWindow where you can see a pushbutton?
-
Tell me: Who is the parent of
w
?I think w is the primary parent of this parent-child-ladder. It's from where this hierarchy of parent-child originates, I think.
@Swati777999 said in Confusion between the usage of 'this' and 'parent':
Tell me: Who is the parent of
w
?I think w is the primary parent of this parent-child-ladder. It's from where this hierarchy of parent-child originates, I think.
Yes, that's correct:
w
is at the top of this parent-child-ladder.So the next question is: Which part of your code in main.cpp set
w
asquit
's parent? Or, asked in a different way: How does main.cpp know thatquit
is not the the "primary parent"?All I want to know, is how can I add widgets in this mainWindow where you can see a pushbutton?
To answer this question, there are actually 2 different things you need to know:
- How to set widget parents
- How to use layouts
These are different different topics.
- #1 is handled by my questions above. Try to answer those questions.
- #2 can be studied from the Basic Layouts Example: https://doc.qt.io/qt-5/qtwidgets-layouts-basiclayouts-example.html Run the example and see.
-
@Swati777999 said in Confusion between the usage of 'this' and 'parent':
Tell me: Who is the parent of
w
?I think w is the primary parent of this parent-child-ladder. It's from where this hierarchy of parent-child originates, I think.
Yes, that's correct:
w
is at the top of this parent-child-ladder.So the next question is: Which part of your code in main.cpp set
w
asquit
's parent? Or, asked in a different way: How does main.cpp know thatquit
is not the the "primary parent"?All I want to know, is how can I add widgets in this mainWindow where you can see a pushbutton?
To answer this question, there are actually 2 different things you need to know:
- How to set widget parents
- How to use layouts
These are different different topics.
- #1 is handled by my questions above. Try to answer those questions.
- #2 can be studied from the Basic Layouts Example: https://doc.qt.io/qt-5/qtwidgets-layouts-basiclayouts-example.html Run the example and see.
@JKSH said in Confusion between the usage of 'this' and 'parent':
So the next question is: Which part of your code in main.cpp set
w
asquit
's parent? Or, asked in a different way: How does main.cpp know thatquit
is not the the "primary parent"?Doesn't LayoutExample w in main.cpp declare w as the parent object?
QPushButton quit("Quit",&w);
This sentence sets the parent of quit to be w.
-
@JKSH said in Confusion between the usage of 'this' and 'parent':
So the next question is: Which part of your code in main.cpp set
w
asquit
's parent? Or, asked in a different way: How does main.cpp know thatquit
is not the the "primary parent"?Doesn't LayoutExample w in main.cpp declare w as the parent object?
QPushButton quit("Quit",&w);
This sentence sets the parent of quit to be w.
@Swati777999 said in Confusion between the usage of 'this' and 'parent':
QPushButton quit("Quit",&w);
This sentence sets the parent of quit to be w.
Correct.
This line constructs a QPushButton called
quit
, and setsw
as the parent of the QPushButton object. This happens because the address ofw
is passed into the QPushButton constructor.Doesn't LayoutExample w in main.cpp declare w as the parent object?
Sort of... I understand what you mean and you are correct, but please be careful with how you use the word "parent".
This line constructs a LayoutExample object called
w
, and sets nothing as the parent of the LayoutExample object. This happens becausenullptr
is passed into the LayoutExample constructor.button1=new QPushButton("Click Here",parent);
Please fill in the blanks (see above for examples):
This line constructs a QPushButton called
button1
, and sets ________ as the parent of the QPushButton object. This happens because ________ is passed into the QPushButton constructor. -
@Swati777999 said in Confusion between the usage of 'this' and 'parent':
QPushButton quit("Quit",&w);
This sentence sets the parent of quit to be w.
Correct.
This line constructs a QPushButton called
quit
, and setsw
as the parent of the QPushButton object. This happens because the address ofw
is passed into the QPushButton constructor.Doesn't LayoutExample w in main.cpp declare w as the parent object?
Sort of... I understand what you mean and you are correct, but please be careful with how you use the word "parent".
This line constructs a LayoutExample object called
w
, and sets nothing as the parent of the LayoutExample object. This happens becausenullptr
is passed into the LayoutExample constructor.button1=new QPushButton("Click Here",parent);
Please fill in the blanks (see above for examples):
This line constructs a QPushButton called
button1
, and sets ________ as the parent of the QPushButton object. This happens because ________ is passed into the QPushButton constructor.@JKSH said in Confusion between the usage of 'this' and 'parent':
@Swati777999 said in Confusion between the usage of 'this' and 'parent':
QPushButton quit("Quit",&w);
This line constructs a LayoutExample object called
w
, and sets nothing as the parent of the LayoutExample object. This happens becausenullptr
is passed into the LayoutExample constructor.button1=new QPushButton("Click Here",parent);
Please fill in the blanks (see above for examples):
This line constructs a QPushButton called
button1
, and sets ________ as the parent of the QPushButton object. This happens because ________ is passed into the QPushButton constructor.Answer to above fill in the blanks: parent, parent (which is set to null pointer)
-
@Swati777999 said in Confusion between the usage of 'this' and 'parent':
QPushButton quit("Quit",&w);
This sentence sets the parent of quit to be w.
Correct.
This line constructs a QPushButton called
quit
, and setsw
as the parent of the QPushButton object. This happens because the address ofw
is passed into the QPushButton constructor.Doesn't LayoutExample w in main.cpp declare w as the parent object?
Sort of... I understand what you mean and you are correct, but please be careful with how you use the word "parent".
This line constructs a LayoutExample object called
w
, and sets nothing as the parent of the LayoutExample object. This happens becausenullptr
is passed into the LayoutExample constructor.button1=new QPushButton("Click Here",parent);
Please fill in the blanks (see above for examples):
This line constructs a QPushButton called
button1
, and sets ________ as the parent of the QPushButton object. This happens because ________ is passed into the QPushButton constructor.In layoutExample.cpp , with the following modification:
setting parent of box, buttons widgets as this and using this->show(), I am able to get the result as below.
However, the layout part seems to have not worked for these widgets.I observed that if I comment line this->show() , quit pushbutton from main.cpp seems to be overlapping signup pushbutton as they're in the same location.
From above observations, I can conclude that whatever design made in mainwindow.cpp overshadows the Main Window of the project. Correct me, if I'm wrong.
LayoutExample::LayoutExample(QWidget *parent) : QMainWindow(parent) { box = new QWidget(this); button1=new QPushButton("Click Here",this); button2 = new QPushButton("Sign up",this); button1->move(200,200); button2->move(100,100); gridLay = new QGridLayout(this); box->setFixedSize(50,50); gridLay->addWidget(box); gridLay->addWidget(button1); gridLay->addWidget(button2); this->setLayout(gridLay); this->show(); //this->parentWidget()->setLayout(gridLay);
-
@JKSH said in Confusion between the usage of 'this' and 'parent':
@Swati777999 said in Confusion between the usage of 'this' and 'parent':
QPushButton quit("Quit",&w);
This line constructs a LayoutExample object called
w
, and sets nothing as the parent of the LayoutExample object. This happens becausenullptr
is passed into the LayoutExample constructor.button1=new QPushButton("Click Here",parent);
Please fill in the blanks (see above for examples):
This line constructs a QPushButton called
button1
, and sets ________ as the parent of the QPushButton object. This happens because ________ is passed into the QPushButton constructor.Answer to above fill in the blanks: parent, parent (which is set to null pointer)
@Swati777999 said in Confusion between the usage of 'this' and 'parent':
Answer to above fill in the blanks: parent, parent (which is set to null pointer)
OK, and what happens when you set
nullptr
as a widget's parent? (Hint: Your LayoutExample object also hasnullptr
as its parent)button1=new QPushButton("Click Here",this);
Good, now you have set your LayoutExample object as the QPushButton's parent. Previously, it had no parent.
However, the layout part seems to have not worked for these widgets.
When you use QGridLayout, you should also specify the row and column: https://doc.qt.io/qt-5/qgridlayout.html#addWidget-1
I observed that if I comment line this->show() , quit pushbutton from main.cpp seems to be overlapping signup pushbutton as they're in the same location.
That is because the QPushButton from main.cpp was not put in a layout. It is good practice to put all child widgets in a layout.
-
@Swati777999 said in Confusion between the usage of 'this' and 'parent':
Answer to above fill in the blanks: parent, parent (which is set to null pointer)
OK, and what happens when you set
nullptr
as a widget's parent? (Hint: Your LayoutExample object also hasnullptr
as its parent)button1=new QPushButton("Click Here",this);
Good, now you have set your LayoutExample object as the QPushButton's parent. Previously, it had no parent.
However, the layout part seems to have not worked for these widgets.
When you use QGridLayout, you should also specify the row and column: https://doc.qt.io/qt-5/qgridlayout.html#addWidget-1
I observed that if I comment line this->show() , quit pushbutton from main.cpp seems to be overlapping signup pushbutton as they're in the same location.
That is because the QPushButton from main.cpp was not put in a layout. It is good practice to put all child widgets in a layout.
@JKSH said in Confusion between the usage of 'this' and 'parent':
@Swati777999 said in Confusion between the usage of 'this' and 'parent':
Answer to above fill in the blanks: parent, parent (which is set to null pointer)
OK, and what happens when you set
nullptr
as a widget's parent? (Hint: Your LayoutExample object also hasnullptr
as its parent)When the widget's parent is set to null, that means it has no parent, maybe it starts acting as a parent.
-
@JKSH said in Confusion between the usage of 'this' and 'parent':
@Swati777999 said in Confusion between the usage of 'this' and 'parent':
Answer to above fill in the blanks: parent, parent (which is set to null pointer)
OK, and what happens when you set
nullptr
as a widget's parent? (Hint: Your LayoutExample object also hasnullptr
as its parent)When the widget's parent is set to null, that means it has no parent, maybe it starts acting as a parent.
@Swati777999 said in Confusion between the usage of 'this' and 'parent':
OK, and what happens when you set
nullptr
as a widget's parent? (Hint: Your LayoutExample object also hasnullptr
as its parent)When the widget's parent is set to null, that means it has no parent,
Correct.
maybe it starts acting as a parent.
That's not quite right. When a widget has no parent, it can act as a top-level window (say "top-level window", not "parent"). That means whenever you call
show()
on a widget that has no parent, then that widget appears in a new window.// button1 and button2 have a parent, and will appear inside your widget button1 = new QPushButton("Click Here", this); button2 = new QPushButton("Sign up",this); // button3 has no parent, and will open in a new window button3 = new QPushButton("Hello"); button3->show(); // If you don't call show(), button3 will be invisible