Why do my nested layouts overlap?

  • Hello,

    This is the result of the code below:


    bars_and_scrollbars_layout_ptr = new QGridLayout(this) ;
            bars_and_scrollbars_layout_ptr->setSpacing(6) ;
            bars_and_scrollbars_layout_ptr->addWidget(ui.qcustomplot_ptr, 0, 0, 3, 3) ;
    control_buttons_layout_ptr = new QGridLayout(this) ;
            control_buttons_layout_ptr->setSpacing(6) ;
    main_layout_ptr = new QVBoxLayout(this) ;
            main_layout_ptr->setSpacing(6) ; 
            main_layout_ptr->insertLayout(0, bars_and_scrollbars_layout_ptr) ;
            main_layout_ptr->insertLayout(1, control_buttons_layout_ptr) ;
    setLayout(main_layout_ptr) ;

    What I want to see is the layout that hosts the buttons (by the way only one is visible for some reason) below the chart.
    What a I forgetting?

    Thanks in advance!

  • Lifetime Qt Champion

    Can't you use the designer for it? It even looks like you're already using a designer-created ui:


    [Edit aha_1980: Fixed typo]

  • Hi,
    I prefer not using the designer.
    Any idea what's going on here?

  • Lifetime Qt Champion


    You are trying to assign every single one of your layouts to the widget. Using this in the constructor makes it equivalent to

    main_layout_ptr = new QGridLayout() ;

    It's likely not what you want.

  • Hi,

    I'm sorry I don't understand: What is it that you suggest I do to get vertical container embed 2 grid containers?

  • Lifetime Qt Champion

    Nope he suggests you do not assign parent in the constructor of the sub-layouts. ( as i read it ;)

    bars_and_scrollbars_layout_ptr = new QGridLayout(this) ;
    control_buttons_layout_ptr = new QGridLayout(this) ;

  • Thank you.
    I am not sure though why this caused the issue above. I solved it by indeed removing "this" and indicating the column span for the buttons.
    But why does indicating parenthood cause this? I find this part to be much harder than in Python based GUI, for example...

  • Lifetime Qt Champion

    As @SGaist says
    assigning a parent widget for the layout is a shortcut for doing

    somelayout = new QGridLayout() ;

    Which is super when its standalone layout but as you have seen give strange side effects
    if later assigned as sub-layout.

    I use mostly Designer as its so quick to design even complex layouts so I didn't even spot it when looking
    at your code.

  • Thank you all for the time and the effort !

  • Lifetime Qt Champion

    Np :)
    Please mark it as solved using the Topic button

Log in to reply