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.4k 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.
  • JKSHJ JKSH

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

    Swati777999S Offline
    Swati777999S Offline
    Swati777999
    wrote on 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

    JKSHJ 1 Reply Last reply
    0
    • Swati777999S Swati777999

      @JKSH

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

      JKSHJ Offline
      JKSHJ Offline
      JKSH
      Moderators
      wrote on last edited by JKSH
      #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
      • 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.

        Swati777999S Offline
        Swati777999S Offline
        Swati777999
        wrote on 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
        0
        • Swati777999S Swati777999

          @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 Offline
          Pl45m4P Offline
          Pl45m4
          wrote on last edited by Pl45m4
          #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

          Swati777999S 2 Replies Last reply
          1
          • Pl45m4P Pl45m4

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

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

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

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

            CASE 2

            where is your this->show() located?! Cant tell right now...

            As mentioned in my previous reply, this->show() is located in layoutExample.cpp file.

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

            Pl45m4P 1 Reply Last reply
            0
            • Swati777999S Swati777999

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

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

              CASE 2

              where is your this->show() located?! Cant tell right now...

              As mentioned in my previous reply, this->show() is located in layoutExample.cpp file.

              Pl45m4P Offline
              Pl45m4P Offline
              Pl45m4
              wrote on last edited by Pl45m4
              #42

              @Swati777999

              Shouldn't make any difference since this in your LayoutExample class and w in main are pretty much the same objects/instances. It's just the point when you show it. Using this->show at the end of c'tor shows the widget right after it is constructed. Then you do some stuff in main() and then w.show() would display it anyway.


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

              ~E. W. Dijkstra

              Swati777999S 1 Reply Last reply
              1
              • Pl45m4P Pl45m4

                @Swati777999

                Shouldn't make any difference since this in your LayoutExample class and w in main are pretty much the same objects/instances. It's just the point when you show it. Using this->show at the end of c'tor shows the widget right after it is constructed. Then you do some stuff in main() and then w.show() would display it anyway.

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

                @Pl45m4

                My doubt is , even if I comment this->show() in layoutExample.cpp , the widgets in layoutExample.cpp still appear on the screen. How is it possible?

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

                JKSHJ Pl45m4P 2 Replies Last reply
                0
                • Pl45m4P Pl45m4

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

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

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

                  @Swati777999 said in Confusion between the usage of 'this' and 'parent':
                  Btw: You should really use your layouts, not create them and then just throw your widgets (buttons) on your parent (LayoutExample window/widget) without actually putting them in a layout.

                  I've used gridlayout for the widgets in layoutExample.cpp , you can check it below(as a part of layoutExample.cpp) but it seems that the layout has not been applied successfully.

                  LayoutExample::LayoutExample(QWidget *parent)
                      : QMainWindow(parent)
                  {
                      box = new QWidget(this);
                      button1=new QPushButton("Click Here",this);
                      button2 = new QPushButton("Sign up",this);
                      button1->move(200,200);
                      button2->move(100,100);
                      gridLay = new QGridLayout(this);
                      box->setFixedSize(50,50);
                  
                      gridLay->addWidget(box);
                      gridLay->addWidget(button1);
                      gridLay->addWidget(button2);
                  
                      this->setLayout(gridLay);
                  
                      this->show();
                  
                      //this->parentWidget()->setLayout(gridLay);
                  }
                  

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

                  JKSHJ 1 Reply Last reply
                  0
                  • Swati777999S Swati777999

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

                    @Swati777999 said in Confusion between the usage of 'this' and 'parent':
                    Btw: You should really use your layouts, not create them and then just throw your widgets (buttons) on your parent (LayoutExample window/widget) without actually putting them in a layout.

                    I've used gridlayout for the widgets in layoutExample.cpp , you can check it below(as a part of layoutExample.cpp) but it seems that the layout has not been applied successfully.

                    LayoutExample::LayoutExample(QWidget *parent)
                        : QMainWindow(parent)
                    {
                        box = new QWidget(this);
                        button1=new QPushButton("Click Here",this);
                        button2 = new QPushButton("Sign up",this);
                        button1->move(200,200);
                        button2->move(100,100);
                        gridLay = new QGridLayout(this);
                        box->setFixedSize(50,50);
                    
                        gridLay->addWidget(box);
                        gridLay->addWidget(button1);
                        gridLay->addWidget(button2);
                    
                        this->setLayout(gridLay);
                    
                        this->show();
                    
                        //this->parentWidget()->setLayout(gridLay);
                    }
                    
                    JKSHJ Offline
                    JKSHJ Offline
                    JKSH
                    Moderators
                    wrote on last edited by JKSH
                    #45

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

                    My doubt here is ; in CASE-2, why should these buttons; Sign up and Click Here appear as this->show() is commented?

                    In CASE-1 , why does quit button from main.cpp appear in the same window?

                    Please explain the reason.

                    I think you've got CASE-1 and CASE-2 mixed up. quit will show if this->show() is commented.

                    The reason is this: When you call show() on a widget, it also automatically calls show() on all of the widget's children.

                    • When you show w before setting it as the parent of quit, then quit won't be shown. You must call quit.show() manually.
                    • When you show w after setting it as the parent of quit, then quit will be automatically shown.

                    But more importantly: You don't need to spend time worrying about things like this. If you just put ALL of your child widgets inside layouts, then you won't encounter these strange behaviours!

                    I've used gridlayout for the widgets... but it seems that the layout has not been applied successfully.

                    Look, both @SimonSchroeder and I have told you already: You must specify the row and column when you call QGridLayout::addWidget(): https://doc.qt.io/qt-5/qgridlayout.html#addWidget-1

                    It irritates us when you ignore our advice and then repeat the same issue.

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

                    Swati777999S 1 Reply Last reply
                    1
                    • JKSHJ JKSH

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

                      My doubt here is ; in CASE-2, why should these buttons; Sign up and Click Here appear as this->show() is commented?

                      In CASE-1 , why does quit button from main.cpp appear in the same window?

                      Please explain the reason.

                      I think you've got CASE-1 and CASE-2 mixed up. quit will show if this->show() is commented.

                      The reason is this: When you call show() on a widget, it also automatically calls show() on all of the widget's children.

                      • When you show w before setting it as the parent of quit, then quit won't be shown. You must call quit.show() manually.
                      • When you show w after setting it as the parent of quit, then quit will be automatically shown.

                      But more importantly: You don't need to spend time worrying about things like this. If you just put ALL of your child widgets inside layouts, then you won't encounter these strange behaviours!

                      I've used gridlayout for the widgets... but it seems that the layout has not been applied successfully.

                      Look, both @SimonSchroeder and I have told you already: You must specify the row and column when you call QGridLayout::addWidget(): https://doc.qt.io/qt-5/qgridlayout.html#addWidget-1

                      It irritates us when you ignore our advice and then repeat the same issue.

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

                      @JKSH

                      Yes, I remember your advice of giving a dimension to my layout to make it appear on the screen, I was mistakenly putting row,column parameters while creating gridLay as a result of which I was getting errors repeatedly but now , I understood the concept.

                      gridLay->addWidget(button1,100,120,Qt::Alignment=0x0020);

                      The third argument gives me an error. How should the value of alignment be used?
                      Qt::Alignment=0x0020

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

                      J.HilkJ 1 Reply Last reply
                      0
                      • Swati777999S Swati777999

                        @JKSH

                        Yes, I remember your advice of giving a dimension to my layout to make it appear on the screen, I was mistakenly putting row,column parameters while creating gridLay as a result of which I was getting errors repeatedly but now , I understood the concept.

                        gridLay->addWidget(button1,100,120,Qt::Alignment=0x0020);

                        The third argument gives me an error. How should the value of alignment be used?
                        Qt::Alignment=0x0020

                        J.HilkJ Offline
                        J.HilkJ Offline
                        J.Hilk
                        Moderators
                        wrote on last edited by
                        #47

                        @Swati777999

                        gridLay->addWidget(button1,100,120,Qt::Alignment=0x0020);
                        

                        100,120 does not mean the pixels on your screen, but row/column 100/120

                        try placing your first widget on column/row 0 and use the actual enum not the integer value as 4th paramter

                        gridLay->addWidget(button1,0,0,Qt::AlignTop);
                        

                        Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                        Q: What's that?
                        A: It's blue light.
                        Q: What does it do?
                        A: It turns blue.

                        Swati777999S 1 Reply Last reply
                        1
                        • J.HilkJ J.Hilk

                          @Swati777999

                          gridLay->addWidget(button1,100,120,Qt::Alignment=0x0020);
                          

                          100,120 does not mean the pixels on your screen, but row/column 100/120

                          try placing your first widget on column/row 0 and use the actual enum not the integer value as 4th paramter

                          gridLay->addWidget(button1,0,0,Qt::AlignTop);
                          
                          Swati777999S Offline
                          Swati777999S Offline
                          Swati777999
                          wrote on last edited by
                          #48

                          @J-Hilk

                          After modifying the existing code, gridLay->addWidget(button1,0,0,Qt::AlignTop);
                          I can't find any change in my display. :(

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

                          1 Reply Last reply
                          0
                          • Swati777999S Swati777999

                            @Pl45m4

                            My doubt is , even if I comment this->show() in layoutExample.cpp , the widgets in layoutExample.cpp still appear on the screen. How is it possible?

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

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

                            My doubt is , even if I comment this->show() in layoutExample.cpp , the widgets in layoutExample.cpp still appear on the screen. How is it possible?

                            Because you called w.show().

                            After modifying the existing code, gridLay->addWidget(button1,0,0,Qt::AlignTop);
                            I can't find any change in my display. :(

                            First: Instead of QMainWindow, make your widget inherit QWidget. This makes things simpler.

                            Second: You only put one button in the layout. You should put ALL buttons in the layout. Remember to use different rows/columns for each button.

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

                            Swati777999S 2 Replies Last reply
                            2
                            • JKSHJ JKSH

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

                              My doubt is , even if I comment this->show() in layoutExample.cpp , the widgets in layoutExample.cpp still appear on the screen. How is it possible?

                              Because you called w.show().

                              After modifying the existing code, gridLay->addWidget(button1,0,0,Qt::AlignTop);
                              I can't find any change in my display. :(

                              First: Instead of QMainWindow, make your widget inherit QWidget. This makes things simpler.

                              Second: You only put one button in the layout. You should put ALL buttons in the layout. Remember to use different rows/columns for each button.

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

                              @JKSH

                              This is the LayoutExample.cpp

                              LayoutExample::LayoutExample(QWidget *parent)
                                  : QMainWindow(parent)
                              {
                              ............
                              }
                              

                              In place of QMainWindow, if I put QWidget, it gives an error. For example:

                              LayoutExample::LayoutExample(QWidget *parent)
                                  : QWidget(parent)
                              {
                              ............
                              }
                              

                              “ 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':

                                My doubt is , even if I comment this->show() in layoutExample.cpp , the widgets in layoutExample.cpp still appear on the screen. How is it possible?

                                Because you called w.show().

                                After modifying the existing code, gridLay->addWidget(button1,0,0,Qt::AlignTop);
                                I can't find any change in my display. :(

                                First: Instead of QMainWindow, make your widget inherit QWidget. This makes things simpler.

                                Second: You only put one button in the layout. You should put ALL buttons in the layout. Remember to use different rows/columns for each button.

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

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

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

                                My doubt is , even if I comment this->show() in layoutExample.cpp , the widgets in layoutExample.cpp still appear on the screen. How is it possible?

                                Because you called w.show().

                                After modifying the existing code, gridLay->addWidget(button1,0,0,Qt::AlignTop);
                                I can't find any change in my display. :(

                                First: Instead of QMainWindow, make your widget inherit QWidget. This makes things simpler.

                                Second: You only put one button in the layout. You should put ALL buttons in the layout. Remember to use different rows/columns for each button.
                                This is the complete code, please refer to it.

                                LayoutExample.cpp

                                LayoutExample::LayoutExample(QWidget *parent)
                                    : QMainWindow(parent)
                                
                                {   gridLay = new QGridLayout(this);
                                    box = new QWidget(this);
                                    button1=new QPushButton("Click Here",this);
                                    button2 = new QPushButton("Sign up",this);
                                    button1->move(200,200);
                                    button2->move(100,100);
                                
                                    gridLay->addWidget(box,60,60);
                                    gridLay->addWidget(button1,0,0,Qt::AlignTop);
                                    gridLay->addWidget(button2,50,100,Qt::AlignBottom);
                                
                                    this->setLayout(gridLay);
                                    this->show();
                                
                                
                                }
                                
                                LayoutExample::~LayoutExample()
                                {
                                }
                                

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

                                1 Reply Last reply
                                0
                                • Swati777999S Swati777999

                                  @JKSH

                                  This is the LayoutExample.cpp

                                  LayoutExample::LayoutExample(QWidget *parent)
                                      : QMainWindow(parent)
                                  {
                                  ............
                                  }
                                  

                                  In place of QMainWindow, if I put QWidget, it gives an error. For example:

                                  LayoutExample::LayoutExample(QWidget *parent)
                                      : QWidget(parent)
                                  {
                                  ............
                                  }
                                  
                                  JKSHJ Offline
                                  JKSHJ Offline
                                  JKSH
                                  Moderators
                                  wrote on last edited by
                                  #52

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

                                  In place of QMainWindow, if I put QWidget, it gives an error.

                                  You must update your .h file too.

                                  gridLay->addWidget(box,60,60);
                                  gridLay->addWidget(button1,0,0,Qt::AlignTop);
                                  gridLay->addWidget(button2,50,100,Qt::AlignBottom);
                                  

                                  You want to specify row/column number. Not pixel number.

                                  I suggest you use a QVBoxLayout for now. It's simpler than QGridLayout.

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

                                  Swati777999S 1 Reply Last reply
                                  2
                                  • JKSHJ JKSH

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

                                    In place of QMainWindow, if I put QWidget, it gives an error.

                                    You must update your .h file too.

                                    gridLay->addWidget(box,60,60);
                                    gridLay->addWidget(button1,0,0,Qt::AlignTop);
                                    gridLay->addWidget(button2,50,100,Qt::AlignBottom);
                                    

                                    You want to specify row/column number. Not pixel number.

                                    I suggest you use a QVBoxLayout for now. It's simpler than QGridLayout.

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

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

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

                                    You must update your .h file too.
                                    Done! It works now!
                                    Layout_Example4.PNG

                                    gridLay->addWidget(box,60,60);
                                    gridLay->addWidget(button1,0,0,Qt::AlignTop);
                                    gridLay->addWidget(button2,50,100,Qt::AlignBottom);
                                    

                                    You want to specify row/column number. Not pixel number.
                                    Noted!

                                    I suggest you use a QVBoxLayout for now. It's simpler than QGridLayout.
                                    Okay

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

                                    1 Reply Last reply
                                    1
                                    • Swati777999S Swati777999

                                      @Pl45m4

                                      My doubt is , even if I comment this->show() in layoutExample.cpp , the widgets in layoutExample.cpp still appear on the screen. How is it possible?

                                      Pl45m4P Offline
                                      Pl45m4P Offline
                                      Pl45m4
                                      wrote on last edited by Pl45m4
                                      #54

                                      @Swati777999

                                      As I've said, because you still show your LayoutExample in your main with w.show().... and this will show the MainWindow widget and ALL its children including your buttons.

                                      Edit:
                                      @JKSH sry :) Missed the sentence in your reply where you also said that


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

                                      ~E. W. Dijkstra

                                      JKSHJ 1 Reply Last reply
                                      1
                                      • Pl45m4P Pl45m4

                                        @Swati777999

                                        As I've said, because you still show your LayoutExample in your main with w.show().... and this will show the MainWindow widget and ALL its children including your buttons.

                                        Edit:
                                        @JKSH sry :) Missed the sentence in your reply where you also said that

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

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

                                        @JKSH sry :) Missed the sentence in your reply where you also said that

                                        No prob! It's good to have multiple confirm the same thing

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

                                        1 Reply Last reply
                                        0

                                        • Login

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