Possible bug in QGridLayout



  • I am working on a tool which needs 3 panels on screen like in the following screenshot:

    wanted layout

    The code I used to recreate that layout is something like

    QGridLayout * layout = new QGridLayout;
    
    QFrame * frame;
    
    frame = new PanelA(this);
    layout->addWidget(frame, 0, 0, 1, 1);
    
    frame = new PanelB(this);
    layout->addWidget(frame, 0, 1, 1, 2);
    
    frame = new PanelC(this);
    layout->addWidget(frame, 1, 0, 1, 3);
    

    All panels inherit from QFrame.

    Everything is fine until the 3 panels are empty, but as soon as I start to add some content to them like for example

    QVBoxLayout * layout = new QVBoxLayout;
    
    layout->addSpacerItem(new QSpacerItem(10, 10, QSizePolicy::Expanding, QSizePolicy::Expanding));
    
    setLayout(layout);
    

    The layout of the main window turns into this:

    wrong layout

    What's really weird is that everything works fine again if I double the column span values and change the window layout code to the following:

    frame = new PanelOpenRepo(this);
    layout->addWidget(frame, 0, 0, 1, 2);
    
    frame = new PanelContributorsSummary(this);
    layout->addWidget(frame, 0, 2, 1, 4);
    
    frame = new PanelContributionCalendar(this);
    layout->addWidget(frame, 1, 0, 1, 6);
    

    This looks a lot like a bug in QGridLayout, but please let me know if I am missing something here.



  • No, it's not a bug, the column span of QGridLayout does not have any link to the relative size of the widgets, it just determines what is put on top of what. To achieve what you want put the top 2 widgets in a QSplitter using setStretchfactor to determine the relative size and then put the QSplitter and the bottom widget in a QHBoxLayout



  • @VRonin said in Possible bug in QGridLayout:

    No, it's not a bug, the column span of QGridLayout does not have any link to the relative size of the widgets, it just determines what is put on top of what.

    Sorry, but that's not true, layout managers are supposed to do exactly that.

    You are right abut this not being a bug though. I was actually missing something: the stretch factor.

    I managed to obtain the layout I wanted with the following code:

    frame = new PanelA(this);
    layout->addWidget(frame, 0, 0, 1, 1);
    
    frame = new PanelB(this);
    layout->addWidget(frame, 0, 1, 1, 1);
    
    frame = new PanelC(this);
    layout->addWidget(frame, 1, 0, 1, 2);
    
    layout->setColumnStretch(0, 1);
    layout->setColumnStretch(1, 2);
    

    Thank you for the alternative solution anyway.


Log in to reply
 

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