Confusion between the usage of 'this' and 'parent'
-
@Swati777999 said in Confusion between the usage of 'this' and 'parent':
In CASE-1 , why does quit button from main.cpp appear in the same window?
Because you've set
w
as parent of yourquit
button. So it appears in parent coodinates. If you remove the&w
from yourquit
button's c'tor and still move it to, say (100, 100) and show it afterwards, it should appear as standalone widget at 100,100 from TOP-LEFT corner of your SCREEN.Btw: You should really use your layouts, not create them and then just throw your widgets (buttons) on your parent (LayoutExample window/widget) without actually putting them in a layout.
CASE 2
where is your
this->show()
located?! Cant tell right now...
The other 2 buttons are there because they are children ofLayoutExample
widget.... and you still show it withw.show()
in yourmain()
@Pl45m4 said in Confusion between the usage of 'this' and 'parent':
@Swati777999 said in Confusion between the usage of 'this' and 'parent':
CASE 2
where is your
this->show()
located?! Cant tell right now...As mentioned in my previous reply,
this->show()
is located in layoutExample.cpp file. -
@Pl45m4 said in Confusion between the usage of 'this' and 'parent':
@Swati777999 said in Confusion between the usage of 'this' and 'parent':
CASE 2
where is your
this->show()
located?! Cant tell right now...As mentioned in my previous reply,
this->show()
is located in layoutExample.cpp file.Shouldn't make any difference since
this
in yourLayoutExample
class andw
inmain
are pretty much the same objects/instances. It's just the point when you show it. Usingthis->show
at the end of c'tor shows the widget right after it is constructed. Then you do some stuff in main() and thenw.show()
would display it anyway. -
Shouldn't make any difference since
this
in yourLayoutExample
class andw
inmain
are pretty much the same objects/instances. It's just the point when you show it. Usingthis->show
at the end of c'tor shows the widget right after it is constructed. Then you do some stuff in main() and thenw.show()
would display it anyway.My doubt is , even if I comment
this->show()
inlayoutExample.cpp
, the widgets inlayoutExample.cpp
still appear on the screen. How is it possible? -
@Swati777999 said in Confusion between the usage of 'this' and 'parent':
In CASE-1 , why does quit button from main.cpp appear in the same window?
Because you've set
w
as parent of yourquit
button. So it appears in parent coodinates. If you remove the&w
from yourquit
button's c'tor and still move it to, say (100, 100) and show it afterwards, it should appear as standalone widget at 100,100 from TOP-LEFT corner of your SCREEN.Btw: You should really use your layouts, not create them and then just throw your widgets (buttons) on your parent (LayoutExample window/widget) without actually putting them in a layout.
CASE 2
where is your
this->show()
located?! Cant tell right now...
The other 2 buttons are there because they are children ofLayoutExample
widget.... and you still show it withw.show()
in yourmain()
@Pl45m4 said in Confusion between the usage of 'this' and 'parent':
@Swati777999 said in Confusion between the usage of 'this' and 'parent':
Btw: You should really use your layouts, not create them and then just throw your widgets (buttons) on your parent (LayoutExample window/widget) without actually putting them in a layout.I've used
gridlayout
for the widgets inlayoutExample.cpp
, you can check it below(as a part oflayoutExample.cpp
) but it seems that the layout has not been applied successfully.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); }
-
@Pl45m4 said in Confusion between the usage of 'this' and 'parent':
@Swati777999 said in Confusion between the usage of 'this' and 'parent':
Btw: You should really use your layouts, not create them and then just throw your widgets (buttons) on your parent (LayoutExample window/widget) without actually putting them in a layout.I've used
gridlayout
for the widgets inlayoutExample.cpp
, you can check it below(as a part oflayoutExample.cpp
) but it seems that the layout has not been applied successfully.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); }
@Swati777999 said in Confusion between the usage of 'this' and 'parent':
My doubt here is ; in CASE-2, why should these buttons; Sign up and Click Here appear as this->show() is commented?
In CASE-1 , why does quit button from main.cpp appear in the same window?
Please explain the reason.
I think you've got CASE-1 and CASE-2 mixed up.
quit
will show ifthis->show()
is commented.The reason is this: When you call
show()
on a widget, it also automatically callsshow()
on all of the widget's children.- When you show
w
before setting it as the parent ofquit
, thenquit
won't be shown. You must callquit.show()
manually. - When you show
w
after setting it as the parent ofquit
, thenquit
will be automatically shown.
But more importantly: You don't need to spend time worrying about things like this. If you just put ALL of your child widgets inside layouts, then you won't encounter these strange behaviours!
I've used
gridlayout
for the widgets... but it seems that the layout has not been applied successfully.Look, both @SimonSchroeder and I have told you already: You must specify the row and column when you call QGridLayout::addWidget(): https://doc.qt.io/qt-5/qgridlayout.html#addWidget-1
It irritates us when you ignore our advice and then repeat the same issue.
- When you show
-
@Swati777999 said in Confusion between the usage of 'this' and 'parent':
My doubt here is ; in CASE-2, why should these buttons; Sign up and Click Here appear as this->show() is commented?
In CASE-1 , why does quit button from main.cpp appear in the same window?
Please explain the reason.
I think you've got CASE-1 and CASE-2 mixed up.
quit
will show ifthis->show()
is commented.The reason is this: When you call
show()
on a widget, it also automatically callsshow()
on all of the widget's children.- When you show
w
before setting it as the parent ofquit
, thenquit
won't be shown. You must callquit.show()
manually. - When you show
w
after setting it as the parent ofquit
, thenquit
will be automatically shown.
But more importantly: You don't need to spend time worrying about things like this. If you just put ALL of your child widgets inside layouts, then you won't encounter these strange behaviours!
I've used
gridlayout
for the widgets... but it seems that the layout has not been applied successfully.Look, both @SimonSchroeder and I have told you already: You must specify the row and column when you call QGridLayout::addWidget(): https://doc.qt.io/qt-5/qgridlayout.html#addWidget-1
It irritates us when you ignore our advice and then repeat the same issue.
Yes, I remember your advice of giving a dimension to my layout to make it appear on the screen, I was mistakenly putting row,column parameters while creating
gridLay
as a result of which I was getting errors repeatedly but now , I understood the concept.gridLay->addWidget(button1,100,120,Qt::Alignment=0x0020);
The third argument gives me an error. How should the value of alignment be used?
Qt::Alignment=0x0020
- When you show
-
Yes, I remember your advice of giving a dimension to my layout to make it appear on the screen, I was mistakenly putting row,column parameters while creating
gridLay
as a result of which I was getting errors repeatedly but now , I understood the concept.gridLay->addWidget(button1,100,120,Qt::Alignment=0x0020);
The third argument gives me an error. How should the value of alignment be used?
Qt::Alignment=0x0020
gridLay->addWidget(button1,100,120,Qt::Alignment=0x0020);
100,120 does not mean the pixels on your screen, but row/column 100/120
try placing your first widget on column/row 0 and use the actual enum not the integer value as 4th paramter
gridLay->addWidget(button1,0,0,Qt::AlignTop);
-
gridLay->addWidget(button1,100,120,Qt::Alignment=0x0020);
100,120 does not mean the pixels on your screen, but row/column 100/120
try placing your first widget on column/row 0 and use the actual enum not the integer value as 4th paramter
gridLay->addWidget(button1,0,0,Qt::AlignTop);
@J-Hilk
After modifying the existing code,
gridLay->addWidget(button1,0,0,Qt::AlignTop);
I can't find any change in my display. :( -
My doubt is , even if I comment
this->show()
inlayoutExample.cpp
, the widgets inlayoutExample.cpp
still appear on the screen. How is it possible?@Swati777999 said in Confusion between the usage of 'this' and 'parent':
My doubt is , even if I comment
this->show()
inlayoutExample.cpp
, the widgets inlayoutExample.cpp
still appear on the screen. How is it possible?Because you called
w.show()
.After modifying the existing code,
gridLay->addWidget(button1,0,0,Qt::AlignTop);
I can't find any change in my display. :(First: Instead of
QMainWindow
, make your widget inheritQWidget
. This makes things simpler.Second: You only put one button in the layout. You should put ALL buttons in the layout. Remember to use different rows/columns for each button.
-
@Swati777999 said in Confusion between the usage of 'this' and 'parent':
My doubt is , even if I comment
this->show()
inlayoutExample.cpp
, the widgets inlayoutExample.cpp
still appear on the screen. How is it possible?Because you called
w.show()
.After modifying the existing code,
gridLay->addWidget(button1,0,0,Qt::AlignTop);
I can't find any change in my display. :(First: Instead of
QMainWindow
, make your widget inheritQWidget
. This makes things simpler.Second: You only put one button in the layout. You should put ALL buttons in the layout. Remember to use different rows/columns for each button.
This is the
LayoutExample.cpp
LayoutExample::LayoutExample(QWidget *parent) : QMainWindow(parent) { ............ }
In place of QMainWindow, if I put QWidget, it gives an error. For example:
LayoutExample::LayoutExample(QWidget *parent) : QWidget(parent) { ............ }
-
@Swati777999 said in Confusion between the usage of 'this' and 'parent':
My doubt is , even if I comment
this->show()
inlayoutExample.cpp
, the widgets inlayoutExample.cpp
still appear on the screen. How is it possible?Because you called
w.show()
.After modifying the existing code,
gridLay->addWidget(button1,0,0,Qt::AlignTop);
I can't find any change in my display. :(First: Instead of
QMainWindow
, make your widget inheritQWidget
. This makes things simpler.Second: You only put one button in the layout. You should put ALL buttons in the layout. Remember to use different rows/columns for each button.
@JKSH said in Confusion between the usage of 'this' and 'parent':
@Swati777999 said in Confusion between the usage of 'this' and 'parent':
My doubt is , even if I comment
this->show()
inlayoutExample.cpp
, the widgets inlayoutExample.cpp
still appear on the screen. How is it possible?Because you called
w.show()
.After modifying the existing code,
gridLay->addWidget(button1,0,0,Qt::AlignTop);
I can't find any change in my display. :(First: Instead of
QMainWindow
, make your widget inheritQWidget
. This makes things simpler.Second: You only put one button in the layout. You should put ALL buttons in the layout. Remember to use different rows/columns for each button.
This is the complete code, please refer to it.LayoutExample.cpp
LayoutExample::LayoutExample(QWidget *parent) : QMainWindow(parent) { gridLay = new QGridLayout(this); 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->addWidget(box,60,60); gridLay->addWidget(button1,0,0,Qt::AlignTop); gridLay->addWidget(button2,50,100,Qt::AlignBottom); this->setLayout(gridLay); this->show(); } LayoutExample::~LayoutExample() { }
-
This is the
LayoutExample.cpp
LayoutExample::LayoutExample(QWidget *parent) : QMainWindow(parent) { ............ }
In place of QMainWindow, if I put QWidget, it gives an error. For example:
LayoutExample::LayoutExample(QWidget *parent) : QWidget(parent) { ............ }
@Swati777999 said in Confusion between the usage of 'this' and 'parent':
In place of QMainWindow, if I put QWidget, it gives an error.
You must update your .h file too.
gridLay->addWidget(box,60,60); gridLay->addWidget(button1,0,0,Qt::AlignTop); gridLay->addWidget(button2,50,100,Qt::AlignBottom);
You want to specify row/column number. Not pixel number.
I suggest you use a QVBoxLayout for now. It's simpler than QGridLayout.
-
@Swati777999 said in Confusion between the usage of 'this' and 'parent':
In place of QMainWindow, if I put QWidget, it gives an error.
You must update your .h file too.
gridLay->addWidget(box,60,60); gridLay->addWidget(button1,0,0,Qt::AlignTop); gridLay->addWidget(button2,50,100,Qt::AlignBottom);
You want to specify row/column number. Not pixel number.
I suggest you use a QVBoxLayout for now. It's simpler than QGridLayout.
@JKSH said in Confusion between the usage of 'this' and 'parent':
@Swati777999 said in Confusion between the usage of 'this' and 'parent':
You must update your .h file too.
Done! It works now!
gridLay->addWidget(box,60,60); gridLay->addWidget(button1,0,0,Qt::AlignTop); gridLay->addWidget(button2,50,100,Qt::AlignBottom);
You want to specify row/column number. Not pixel number.
Noted!
I suggest you use a QVBoxLayout for now. It's simpler than QGridLayout.
Okay -
My doubt is , even if I comment
this->show()
inlayoutExample.cpp
, the widgets inlayoutExample.cpp
still appear on the screen. How is it possible?As I've said, because you still show your
LayoutExample
in your main withw.show()
.... and this will show the MainWindow widget and ALL its children including your buttons.Edit:
@JKSH sry :) Missed the sentence in your reply where you also said that -
As I've said, because you still show your
LayoutExample
in your main withw.show()
.... and this will show the MainWindow widget and ALL its children including your buttons.Edit:
@JKSH sry :) Missed the sentence in your reply where you also said that@Pl45m4 said in Confusion between the usage of 'this' and 'parent':
@JKSH sry :) Missed the sentence in your reply where you also said that
No prob! It's good to have multiple confirm the same thing