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. Confusion between the usage of 'this' and 'parent'
Forum Updated to NodeBB v4.3 + New Features

Confusion between the usage of 'this' and 'parent'

Scheduled Pinned Locked Moved Solved General and Desktop
55 Posts 9 Posters 9.3k Views 3 Watching
  • 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.
  • Swati777999S Swati777999

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

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

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

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

    1 Reply Last reply
    1
    • Swati777999S Swati777999

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

      Christian EhrlicherC Online
      Christian EhrlicherC Online
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #8

      @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

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      Swati777999S 1 Reply Last reply
      2
      • R Rozhin_so

        @jsulm Yes you are right. But for getting the main idea of what parent means , she can read that at first.

        Swati777999S Offline
        Swati777999S Offline
        Swati777999
        wrote on last edited by
        #9

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

        “ In order to be irreplaceable, one must always be different” – Coco Chanel

        jsulmJ JonBJ 2 Replies Last reply
        0
        • Swati777999S Swati777999

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

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

          @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

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

          Swati777999S 1 Reply Last reply
          3
          • Christian EhrlicherC Christian Ehrlicher

            @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

            Swati777999S Offline
            Swati777999S Offline
            Swati777999
            wrote on last edited by
            #11

            @Christian-Ehrlicher

            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?

            “ In order to be irreplaceable, one must always be different” – Coco Chanel

            jsulmJ 1 Reply Last reply
            0
            • Swati777999S Swati777999

              @Christian-Ehrlicher

              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?

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

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

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

              1 Reply Last reply
              1
              • Swati777999S Swati777999

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

                JKSHJ Offline
                JKSHJ Offline
                JKSH
                Moderators
                wrote on last edited by JKSH
                #13

                @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 was nullptr. 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?

                Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

                Swati777999S 2 Replies Last reply
                1
                • JKSHJ JKSH

                  @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 was nullptr. 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?

                  Swati777999S Offline
                  Swati777999S Offline
                  Swati777999
                  wrote on last edited by
                  #14

                  @JKSH

                  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?

                  “ In order to be irreplaceable, one must always be different” – Coco Chanel

                  jsulmJ JKSHJ 2 Replies Last reply
                  0
                  • Swati777999S Swati777999

                    @JKSH

                    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?

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

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

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

                    1 Reply Last reply
                    1
                    • Swati777999S Swati777999

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

                      JonBJ Offline
                      JonBJ Offline
                      JonB
                      wrote on last edited by
                      #16

                      @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/access parent, 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!

                      1 Reply Last reply
                      0
                      • JKSHJ JKSH

                        @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 was nullptr. 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?

                        Swati777999S Offline
                        Swati777999S Offline
                        Swati777999
                        wrote on last edited by
                        #17

                        @JKSH

                        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.

                        “ In order to be irreplaceable, one must always be different” – Coco Chanel

                        jsulmJ JKSHJ 2 Replies Last reply
                        0
                        • Swati777999S Swati777999

                          @JKSH

                          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.

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

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

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

                          1 Reply Last reply
                          1
                          • jsulmJ jsulm

                            @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

                            Swati777999S Offline
                            Swati777999S Offline
                            Swati777999
                            wrote on last edited by
                            #19

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

                            Yes, 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.

                            “ In order to be irreplaceable, one must always be different” – Coco Chanel

                            jsulmJ 1 Reply Last reply
                            2
                            • Swati777999S Swati777999

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

                              Yes, 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.

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

                              @Swati777999 I upvoted your last post. Now you should be able to answer faster :-)

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

                              Swati777999S 1 Reply Last reply
                              2
                              • jsulmJ jsulm

                                @Swati777999 I upvoted your last post. Now you should be able to answer faster :-)

                                Swati777999S Offline
                                Swati777999S Offline
                                Swati777999
                                wrote on last edited by
                                #21

                                @jsulm
                                So kind of you! :)

                                “ In order to be irreplaceable, one must always be different” – Coco Chanel

                                1 Reply Last reply
                                0
                                • Swati777999S Swati777999

                                  @JKSH

                                  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?

                                  JKSHJ Offline
                                  JKSHJ Offline
                                  JKSH
                                  Moderators
                                  wrote on last edited by JKSH
                                  #22

                                  @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 of quit. In other words, quit is a child of w.

                                  Tell me: Who is the parent of w?

                                  Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

                                  Swati777999S 1 Reply Last reply
                                  1
                                  • Swati777999S Swati777999

                                    @JKSH

                                    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.

                                    JKSHJ Offline
                                    JKSHJ Offline
                                    JKSH
                                    Moderators
                                    wrote on last edited by
                                    #23

                                    @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 pointer parent should point to w.

                                    @Swati777999, note that in your code parent->setLayout(gridLay); is the same as this->parentWidget()->setLayout(gridLay);

                                    Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

                                    Swati777999S 1 Reply Last reply
                                    0
                                    • JKSHJ JKSH

                                      @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 of quit. In other words, quit is a child of w.

                                      Tell me: Who is the parent of w?

                                      Swati777999S Offline
                                      Swati777999S Offline
                                      Swati777999
                                      wrote on last edited by
                                      #24

                                      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.

                                      “ In order to be irreplaceable, one must always be different” – Coco Chanel

                                      JKSHJ 1 Reply Last reply
                                      0
                                      • JKSHJ JKSH

                                        @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 pointer parent should point to w.

                                        @Swati777999, note that in your code parent->setLayout(gridLay); is the same as this->parentWidget()->setLayout(gridLay);

                                        Swati777999S Offline
                                        Swati777999S Offline
                                        Swati777999
                                        wrote on last edited by Swati777999
                                        #25

                                        @JKSH

                                        On running the above code! Layout_Example.PNG, I get the following result.

                                        Layout_Example.PNG

                                        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?

                                        “ In order to be irreplaceable, one must always be different” – Coco Chanel

                                        1 Reply Last reply
                                        0
                                        • Swati777999S Swati777999

                                          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.

                                          JKSHJ Offline
                                          JKSHJ Offline
                                          JKSH
                                          Moderators
                                          wrote on last edited by
                                          #26

                                          @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 as quit's parent? Or, asked in a different way: How does main.cpp know that quit 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:

                                          1. How to set widget parents
                                          2. 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.

                                          Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

                                          Swati777999S 1 Reply Last reply
                                          2

                                          • Login

                                          • Login or register to search.
                                          • First post
                                            Last post
                                          0
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular
                                          • Users
                                          • Groups
                                          • Search
                                          • Get Qt Extensions
                                          • Unsolved