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. Child Layouts not getting added Properly
Forum Updated to NodeBB v4.3 + New Features

Child Layouts not getting added Properly

Scheduled Pinned Locked Moved Solved General and Desktop
15 Posts 4 Posters 1.7k Views 1 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.
  • A Offline
    A Offline
    Adit
    wrote on last edited by
    #1

    I'm new to qt and exploring it .Basically Im adding three child layouts to a Parent layout.But after having added the third layout,my first layout disappears. This is my code:

    MainWindow::MainWindow(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::MainWindow)
    {

    this->setGeometry(500,650,1000,1000);

    QVBoxLayout *parentLayout = new QVBoxLayout(this);

    QVBoxLayout *l1 = new QVBoxLayout();
    QWidget *lw1 = new QWidget;
    lw1->setMaximumHeight(50);
    lw1->setStyleSheet("background-color:brown");
    l1->addWidget(lw1);

    QHBoxLayout *l2 = new QHBoxLayout;

    QLabel *label = new QLabel("Industry");
    label->setStyleSheet("color:white");
    label->setMaximumWidth(300);
    l2->addWidget(label);

    QComboBox *cb = new QComboBox;
    cb->addItem("Movie");
    cb->setStyleSheet("background-color:white");
    cb->setMaximumHeight(50);
    l2->addWidget(cb);

    parentLayout->addLayout(l1);
    parentLayout->addLayout(l2);

    //If I run till here I see the added two child layouts.

    QWidget *w3 = new QWidget;
    w3->setStyleSheet("background-color:white");

    QVBoxLayout *l3 = new QVBoxLayout();
    l3->addWidget(w3);

    //parentLayout->addLayout(l3);
    //If I uncomment the above line then I see only layouts l2 and l3 from the top and not l1

    }

    Basically what I need is a Layout which in turn containing multiple subLayouts of varied sizes.Could someone highlight me where am I doing mistake.Any help will be really useful.

    J.HilkJ 1 Reply Last reply
    0
    • A Adit

      I'm new to qt and exploring it .Basically Im adding three child layouts to a Parent layout.But after having added the third layout,my first layout disappears. This is my code:

      MainWindow::MainWindow(QWidget *parent) :
      QWidget(parent),
      ui(new Ui::MainWindow)
      {

      this->setGeometry(500,650,1000,1000);

      QVBoxLayout *parentLayout = new QVBoxLayout(this);

      QVBoxLayout *l1 = new QVBoxLayout();
      QWidget *lw1 = new QWidget;
      lw1->setMaximumHeight(50);
      lw1->setStyleSheet("background-color:brown");
      l1->addWidget(lw1);

      QHBoxLayout *l2 = new QHBoxLayout;

      QLabel *label = new QLabel("Industry");
      label->setStyleSheet("color:white");
      label->setMaximumWidth(300);
      l2->addWidget(label);

      QComboBox *cb = new QComboBox;
      cb->addItem("Movie");
      cb->setStyleSheet("background-color:white");
      cb->setMaximumHeight(50);
      l2->addWidget(cb);

      parentLayout->addLayout(l1);
      parentLayout->addLayout(l2);

      //If I run till here I see the added two child layouts.

      QWidget *w3 = new QWidget;
      w3->setStyleSheet("background-color:white");

      QVBoxLayout *l3 = new QVBoxLayout();
      l3->addWidget(w3);

      //parentLayout->addLayout(l3);
      //If I uncomment the above line then I see only layouts l2 and l3 from the top and not l1

      }

      Basically what I need is a Layout which in turn containing multiple subLayouts of varied sizes.Could someone highlight me where am I doing mistake.Any help will be really useful.

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

      hi,

      I assume this is the default MainWindow from QtCreator ProjectWizard.

      in that case, this already has a layout and you can not simply assign a new one

      change this

      @Adit said in Child Layouts not getting added Properly:

      QVBoxLayout *parentLayout = new QVBoxLayout(this);

      to

      QVBoxLayout *parentLayout = new QVBoxLayout(ui->centralWidget);


      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.

      1 Reply Last reply
      1
      • JonBJ Offline
        JonBJ Offline
        JonB
        wrote on last edited by JonB
        #3

        Following on from @J-Hilk's doubtless-correct analysis:

        @Adit

        QVBoxLayout *parentLayout = new QVBoxLayout(this);

        If you ran this from a debugger, did you not get a warning message saying "warning adding a layout to ... which already has an existing layout"?

        @J-Hilk

        QVBoxLayout *parentLayout = new QVBoxLayout(ui->centralWidget);

        Is this the same as

        QVBoxLayout *parentLayout = new QVBoxLayout();
        ui->centralWidget->setLayout(parentLayout);
        

        ?

        J.HilkJ 1 Reply Last reply
        0
        • JonBJ JonB

          Following on from @J-Hilk's doubtless-correct analysis:

          @Adit

          QVBoxLayout *parentLayout = new QVBoxLayout(this);

          If you ran this from a debugger, did you not get a warning message saying "warning adding a layout to ... which already has an existing layout"?

          @J-Hilk

          QVBoxLayout *parentLayout = new QVBoxLayout(ui->centralWidget);

          Is this the same as

          QVBoxLayout *parentLayout = new QVBoxLayout();
          ui->centralWidget->setLayout(parentLayout);
          

          ?

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

          @JonB said in Child Layouts not getting added Properly:

          @J-Hilk

          QVBoxLayout *parentLayout = new QVBoxLayout(ui->centralWidget);

          Is this the same as
          QVBoxLayout *parentLayout = new QVBoxLayout();
          ui->centralWidget.seLayout(parentLayout);

          ?

          yes, with the setLayout call, the Layout gets reparented to centralWidget.


          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.

          1 Reply Last reply
          1
          • A Offline
            A Offline
            Adit
            wrote on last edited by Adit
            #5

            Guys , here my MainWindow is not a QMainWindow,instead ,it inherits from QWidget.Moreover I dont use the .ui form and I create the UI dynamically.

            J.HilkJ 1 Reply Last reply
            0
            • A Adit

              Guys , here my MainWindow is not a QMainWindow,instead ,it inherits from QWidget.Moreover I dont use the .ui form and I create the UI dynamically.

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

              @Adit
              what's the ui-pointer for than?

              MainWindow::MainWindow(QWidget *parent) :
              QWidget(parent),
              ui(new Ui::MainWindow)
              {


              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.

              1 Reply Last reply
              0
              • A Offline
                A Offline
                Adit
                wrote on last edited by
                #7

                Sorry @J-Hilk ,but Im not using it .I tried to add both setMinimumHeight and setMaximumHeight and it works now.Could you tell me when these two functions should be used?

                jsulmJ 1 Reply Last reply
                0
                • A Adit

                  Sorry @J-Hilk ,but Im not using it .I tried to add both setMinimumHeight and setMaximumHeight and it works now.Could you tell me when these two functions should be used?

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

                  @Adit What about http://doc.qt.io/qt-5/qwidget.html#setLayout ?
                  You created a layout (parentLayout), but you did not tell your MainWindow widget to use it.

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

                  1 Reply Last reply
                  0
                  • A Offline
                    A Offline
                    Adit
                    wrote on last edited by
                    #9

                    But I have set the MainWindow widget as parent to the Top-Layout created and it works!

                    J.HilkJ 1 Reply Last reply
                    0
                    • A Adit

                      But I have set the MainWindow widget as parent to the Top-Layout created and it works!

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

                      @Adit
                      It gets a bit tricky when you add Layouts to Layouts.

                      it should also work, if you specify strechfactors for the addes layouts e.g

                      parentLayout->setStretch(0,1);
                      parentLayout->setStretch(1,1);
                      parentLayout->setStretch(2,1);
                      

                      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.

                      1 Reply Last reply
                      0
                      • A Offline
                        A Offline
                        Adit
                        wrote on last edited by
                        #11

                        I guess stretch factors can be 0 or 1 (contract and expand) right?What if we have multiple Layouts of specific sizes ?

                        J.HilkJ 1 Reply Last reply
                        0
                        • A Adit

                          I guess stretch factors can be 0 or 1 (contract and expand) right?What if we have multiple Layouts of specific sizes ?

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

                          @Adit said in Child Layouts not getting added Properly:

                          I guess stretch factors can be 0 or 1 (contract and expand) right?What if we have multiple Layouts of specific sizes ?

                          no, 0 means, take as much space, as possible, defined by the sizepolicy and sizehint of the Widget/Layout

                          in this case a stretch factor of 1 for each element results in the following space for the layouts, 1/3, 1/3 and 1/3.

                          With for example

                          parentLayout->setStretch(0,1);
                          parentLayout->setStretch(1,1);
                          parentLayout->setStretch(2,2);
                          

                          the layouts take up 1/4, 1/4, 1/2 of the available space.


                          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.

                          1 Reply Last reply
                          0
                          • A Offline
                            A Offline
                            Adit
                            wrote on last edited by
                            #13

                            If we are using this setStretch() method, then can we use setMaximumWidth() and setMaximumHeight() methods?

                            J.HilkJ 1 Reply Last reply
                            0
                            • A Adit

                              If we are using this setStretch() method, then can we use setMaximumWidth() and setMaximumHeight() methods?

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

                              @Adit yes, the stretch is applied until min and max sizes are hit and as long as no contradicting QSizePolizies are set (like fixed size)


                              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.

                              1 Reply Last reply
                              1
                              • A Offline
                                A Offline
                                Adit
                                wrote on last edited by
                                #15

                                Could you suggest me some idea to build a complex layout(i.e sublayouts of varying sizes)?Could you suggest me an opensource project for building a resolution independent UI.

                                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