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'

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

Scheduled Pinned Locked Moved Solved General and Desktop
55 Posts 9 Posters 7.9k Views
  • 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.
  • J jsulm
    7 Dec 2021, 09:58

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

    S Offline
    S Offline
    Swati777999
    wrote on 7 Dec 2021, 10:01 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
    • S Swati777999
      7 Dec 2021, 09:20

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

      J Offline
      J Offline
      JKSH
      Moderators
      wrote on 7 Dec 2021, 11:31 last edited by JKSH 12 Jul 2021, 11:35
      #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

      S 1 Reply Last reply 8 Dec 2021, 01:17
      1
      • S Swati777999
        7 Dec 2021, 09:37

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

        J Offline
        J Offline
        JKSH
        Moderators
        wrote on 7 Dec 2021, 23:20 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

        S 1 Reply Last reply 8 Dec 2021, 03:26
        0
        • J JKSH
          7 Dec 2021, 11:31

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

          S Offline
          S Offline
          Swati777999
          wrote on 8 Dec 2021, 01:17 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

          J 1 Reply Last reply 8 Dec 2021, 03:38
          0
          • J JKSH
            7 Dec 2021, 23:20

            @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);

            S Offline
            S Offline
            Swati777999
            wrote on 8 Dec 2021, 03:26 last edited by Swati777999 12 Aug 2021, 03:26
            #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
            • S Swati777999
              8 Dec 2021, 01:17

              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.

              J Offline
              J Offline
              JKSH
              Moderators
              wrote on 8 Dec 2021, 03:38 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

              S 1 Reply Last reply 8 Dec 2021, 04:10
              2
              • J JKSH
                8 Dec 2021, 03:38

                @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.
                S Offline
                S Offline
                Swati777999
                wrote on 8 Dec 2021, 04:10 last edited by Swati777999 12 Aug 2021, 04:12
                #27

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

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

                J 1 Reply Last reply 8 Dec 2021, 04:52
                0
                • S Swati777999
                  8 Dec 2021, 04:10

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

                  J Offline
                  J Offline
                  JKSH
                  Moderators
                  wrote on 8 Dec 2021, 04:52 last edited by JKSH 12 Aug 2021, 04:58
                  #28

                  @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 sets w as the parent of the QPushButton object. This happens because the address of w 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 because nullptr 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.

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

                  S 2 Replies Last reply 8 Dec 2021, 06:01
                  3
                  • J JKSH
                    8 Dec 2021, 04:52

                    @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 sets w as the parent of the QPushButton object. This happens because the address of w 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 because nullptr 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.

                    S Offline
                    S Offline
                    Swati777999
                    wrote on 8 Dec 2021, 06:01 last edited by
                    #29

                    @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 because nullptr 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)

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

                    J 1 Reply Last reply 8 Dec 2021, 06:17
                    0
                    • J JKSH
                      8 Dec 2021, 04:52

                      @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 sets w as the parent of the QPushButton object. This happens because the address of w 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 because nullptr 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.

                      S Offline
                      S Offline
                      Swati777999
                      wrote on 8 Dec 2021, 06:09 last edited by
                      #30

                      @JKSH

                      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.

                      layout_Example2.PNG

                      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);
                      

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

                      1 Reply Last reply
                      0
                      • S Swati777999
                        8 Dec 2021, 06:01

                        @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 because nullptr 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)

                        J Offline
                        J Offline
                        JKSH
                        Moderators
                        wrote on 8 Dec 2021, 06:17 last edited by
                        #31

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

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

                        S 1 Reply Last reply 8 Dec 2021, 06:26
                        2
                        • J JKSH
                          8 Dec 2021, 06:17

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

                          S Offline
                          S Offline
                          Swati777999
                          wrote on 8 Dec 2021, 06:26 last edited by
                          #32

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

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

                          J 1 Reply Last reply 8 Dec 2021, 06:51
                          0
                          • S Swati777999
                            8 Dec 2021, 06:26

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

                            J Offline
                            J Offline
                            JKSH
                            Moderators
                            wrote on 8 Dec 2021, 06:51 last edited by
                            #33

                            @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 has nullptr 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
                            

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

                            S 1 Reply Last reply 8 Dec 2021, 07:33
                            4
                            • J JKSH
                              8 Dec 2021, 06:51

                              @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 has nullptr 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
                              
                              S Offline
                              S Offline
                              Swati777999
                              wrote on 8 Dec 2021, 07:33 last edited by Swati777999 12 Aug 2021, 07:33
                              #34

                              @JKSH said in Confusion between the usage of 'this' and 'parent':

                              @Swati777999 said in Confusion between the usage of 'this' and 'parent':

                              button1 and button2 appear on the window because I have used this->show() at the end.

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

                              I am not worried about button3, all I am concerned about is quit button of main.cpp file.

                              I made following change in main.cpp

                              quit.move(80,80)
                              

                              and w.show() is already there in main.cpp

                              Then, why isn't quit button showing up in my window whereas other two buttons signup and 'Click Here' appear?

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

                              J 1 Reply Last reply 8 Dec 2021, 12:44
                              0
                              • S Offline
                                S Offline
                                SimonSchroeder
                                wrote on 8 Dec 2021, 07:56 last edited by
                                #35

                                You shouldn't move and resize widgets (buttons are also widgets) when you want to use layouts. Layouts will position and resize all their widgets themselves. Most likely layouts will override everything else in the end.

                                When you are using a QGridLayout you should always specify the row and column. I am not sure what would happen otherwise. Only with the box layouts it makes sense to not specify a position in the layout as they will then only be appended (to the right in horizontal layouts or to the bottom in vertical layouts).

                                About the parents in Qt: Unfortunately, it is a little bit more confusing when you are using layouts than what you have discussed so far. Whenever you add a widget to a layout it will be reparented to the layout's parent. The reason behind this is that for widgets the parent-child relationship also applies to the visual representation. When you show a parent all its children will be shown as well. I personally don't provide a parent when creating child objects (like in new QPushButton("Button")) but rely on the parent being set when I add widgets to a layout (and I exclusively use layouts instead of fixed positioning). Though this is just my personal preference.

                                1 Reply Last reply
                                1
                                • S Swati777999
                                  8 Dec 2021, 07:33

                                  @JKSH said in Confusion between the usage of 'this' and 'parent':

                                  @Swati777999 said in Confusion between the usage of 'this' and 'parent':

                                  button1 and button2 appear on the window because I have used this->show() at the end.

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

                                  I am not worried about button3, all I am concerned about is quit button of main.cpp file.

                                  I made following change in main.cpp

                                  quit.move(80,80)
                                  

                                  and w.show() is already there in main.cpp

                                  Then, why isn't quit button showing up in my window whereas other two buttons signup and 'Click Here' appear?

                                  J Offline
                                  J Offline
                                  JKSH
                                  Moderators
                                  wrote on 8 Dec 2021, 12:44 last edited by
                                  #36

                                  @Swati777999 said in Confusion between the usage of 'this' and 'parent':

                                  Then, why isn't quit button showing up in my window whereas other two buttons signup and 'Click Here' appear?

                                  I agree with @SimonSchroeder: Since your top-level widget has a layout, do not add a child widget that is not part of that layout. It is not designed to work that way.

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

                                  S 1 Reply Last reply 9 Dec 2021, 01:14
                                  0
                                  • J JKSH
                                    8 Dec 2021, 12:44

                                    @Swati777999 said in Confusion between the usage of 'this' and 'parent':

                                    Then, why isn't quit button showing up in my window whereas other two buttons signup and 'Click Here' appear?

                                    I agree with @SimonSchroeder: Since your top-level widget has a layout, do not add a child widget that is not part of that layout. It is not designed to work that way.

                                    S Offline
                                    S Offline
                                    Swati777999
                                    wrote on 9 Dec 2021, 01:14 last edited by
                                    #37

                                    @JKSH

                                    What do you mean by top-level widget? Is it the one declared in main.cpp ; i.e quit button?

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

                                    J 1 Reply Last reply 9 Dec 2021, 01:22
                                    0
                                    • S Swati777999
                                      9 Dec 2021, 01:14

                                      @JKSH

                                      What do you mean by top-level widget? Is it the one declared in main.cpp ; i.e quit button?

                                      J Offline
                                      J Offline
                                      JKSH
                                      Moderators
                                      wrote on 9 Dec 2021, 01:22 last edited by JKSH 12 Sept 2021, 01:25
                                      #38

                                      @Swati777999 said in Confusion between the usage of 'this' and 'parent':

                                      @JKSH

                                      What do you mean by top-level widget? Is it the one declared in main.cpp ; i.e quit button?

                                      A top-level widget is a widget that has its own window.

                                      In your code, button1 and button2 are not top-level widgets because they don't have their own windows. Instead, they are inside w's window.

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

                                      1 Reply Last reply
                                      1
                                      • S Swati777999
                                        7 Dec 2021, 08:01

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

                                        S Offline
                                        S Offline
                                        Swati777999
                                        wrote on 9 Dec 2021, 01:28 last edited by
                                        #39

                                        @Swati777999 said in Confusion between the usage of 'this' and 'parent':

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

                                        @JKSH
                                        Another observation here,
                                        In layoutExample.cpp , [ in main.cpp , w.show() is present as it is ]
                                        CASE-1
                                        In the presence of

                                        this->show();
                                        
                                        

                                        following result is obtained ;
                                        Layout_Example3.PNG

                                        CASE-2
                                        When I comment

                                        //this->show();
                                        

                                        I get the following result
                                        layout_Example2.PNG

                                        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.

                                        Thanks!

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

                                        Pl45m4P 1 Reply Last reply 9 Dec 2021, 03:24
                                        0
                                        • S Swati777999
                                          9 Dec 2021, 01:28

                                          @Swati777999 said in Confusion between the usage of 'this' and 'parent':

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

                                          @JKSH
                                          Another observation here,
                                          In layoutExample.cpp , [ in main.cpp , w.show() is present as it is ]
                                          CASE-1
                                          In the presence of

                                          this->show();
                                          
                                          

                                          following result is obtained ;
                                          Layout_Example3.PNG

                                          CASE-2
                                          When I comment

                                          //this->show();
                                          

                                          I get the following result
                                          layout_Example2.PNG

                                          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.

                                          Thanks!

                                          Pl45m4P Online
                                          Pl45m4P Online
                                          Pl45m4
                                          wrote on 9 Dec 2021, 03:24 last edited by Pl45m4 12 Sept 2021, 03:35
                                          #40

                                          @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 your quit button. So it appears in parent coodinates. If you remove the &w from your quit 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 of LayoutExample widget.... and you still show it with w.show() in your main()


                                          If debugging is the process of removing software bugs, then programming must be the process of putting them in.

                                          ~E. W. Dijkstra

                                          S 2 Replies Last reply 9 Dec 2021, 03:34
                                          1

                                          30/55

                                          8 Dec 2021, 06:09

                                          • Login

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