Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Why do my nested layouts overlap?



  • Hello,

    This is the result of the code below:

    0_1556385628361_aa37a72d-34cd-493e-ad39-085bfb97ef6a-image.png

    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!


  • Qt Champions 2019

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

    ui.qcustomplot_ptr

    [Edit aha_1980: Fixed typo]



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


  • Lifetime Qt Champion

    Hi,

    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() ;
    setLayout(main_layout_ptr)
    

    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

    Hi
    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

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

    somelayout = new QGridLayout() ;
    setLayout(somelayout);

    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

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


Log in to reply