Confusion between the usage of 'this' and 'parent'
-
Hi All ,
I am facing confusion in using 'this' and 'parent' as a result of which I am not getting any output in my window.
Can someone differentiate between these for me?Thanks in advance!
-
@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':
Can someone differentiate between these for me?
this is the pointer to your current class instance, parent is a pointer to another class instance, mostly the parent of the current class when you follow to parent-child relationship of Qt..
Please go and read a c++ book. -
@Rozhin_so said in Confusion between the usage of 'this' and 'parent':
search for "inheritance" ( in c++)
Parent/child concept in Qt has nothing to do with inheritance.
It is used to build object trees, see https://doc.qt.io/qt-5/objecttrees.html -
@Christian-Ehrlicher
There's only one example in the documentation.The following code is something I tried on my own.
layoutexample.h
#ifndef LAYOUTEXAMPLE_H #define LAYOUTEXAMPLE_H #include <QMainWindow> #include <QWidget> #include <QTableWidget> #include <QGridLayout> #include <QPushButton> #include <QMenuBar> class LayoutExample : public QMainWindow { Q_OBJECT QWidget *box; QGridLayout *gridLay; QPushButton *button1; QPushButton *button2; public: LayoutExample(QWidget *parent=nullptr); ~LayoutExample(); }; #endif // LAYOUTEXAMPLE_H
layoutexample.cpp
#include "layoutexample.h" #include <QGridLayout> #include <QWidget> #include <QTableWidget> #include <QPushButton> #include <QMenuBar> LayoutExample::LayoutExample(QWidget *parent) : QMainWindow(parent) { box = new QWidget(); button1=new QPushButton("Click Here"); button2 = new QPushButton("Sign up"); gridLay = new QGridLayout(); box->setFixedSize(100,100); gridLay->setRowMinimumHeight(5,4); gridLay->setHorizontalSpacing(20); gridLay->addWidget(box); gridLay->addWidget(button1); gridLay->addWidget(button2); this->setLayout(gridLay); //this->parentWidget()->setLayout(gridLay); } LayoutExample::~LayoutExample() { }
In the above code, in place of 'this' when I write parent , my program crashes. This code does not add widgets to the main window. Only the effects of codes of main.cpp are reflected in the main window.
How can I use w parent Qobject from main.cpp in layout.cpp? [I am so confused about the structure of writing codes in these 3 files and parent-child implementation]
Does parent in layout.cpp refer to w ?
main.cpp#include "layoutexample.h" #include <QApplication> #include <QWidget> #include <QTableWidget> #include <QMenuBar> int main(int argc, char *argv[]) { QApplication a(argc, argv); LayoutExample w; // w: parent QObject QPushButton quit("Quit",&w); // quit: child QObject quit.move(100,100); // x and y coordinates of widgets wrt to the mainwindow w.setWindowTitle("Layout Example"); quit.setBaseSize(100,100); quit.setAutoFillBackground("yes"); QGridLayout grid(&w); //w.setLayout(grid); w.show(); return a.exec(); } }
Edit: I've changed the name of the project from layout to layoutExample to avoid confusion.
-
@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
Do you mean this line:
this->setLayout(gridLay);
?
Of course it will crash if you replace this with parent and parent is a nullptr (which it is by default, unless you pass a pointer to a valid object), C++ basics.
But why would you want to do so?
Child should not change its parent, else it is bad design. -
@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.
I would be surprised if not.
If you want to know about layouts then read https://doc.qt.io/qt-5/layout.html
And calling the MainWindow 'layout' is somewhat... strange
-
@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':
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 -
I named mainwindow as the name of the project;layout while creating it. Later, I found it to be confusing.
Then how can I add widgets to the mainwindow; the object of layout which is w that I've created?
-
@Swati777999 said in Confusion between the usage of 'this' and 'parent':
Then how can I add widgets to the mainwindow
You do it in mainwindow, not in its children.
In mainwindow you create its children and add them to the layout. -
@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? -
@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->... }
-
@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! -
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... -
@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 :-)