Child Layouts not getting added Properly



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



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



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

    ?



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



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



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

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



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


  • Moderators

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



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



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


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



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



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



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



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


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.