Important: Please read the Qt Code of Conduct -

QLayout is wasting space

  • I add Qwidgets dynamically

    for(int i = 0; i < value; i++)
    QList<QWidget*> widgetList_i;
    //... widgetList_i.append(a lot of widgets)

    during runtime to my app. As you can see here there is a lot of space wasted between those QWidgets. I am able to drag the border between the two dockWidgets and compress the lower layout like here

    But I do not want to do that manually. How can I start a layout already compressed?

    EDIT: In short, I expect two things:

    1. Start that lower dockWidget/layout compressed, with no space wasting.
    2. After starting is should be possible to increase its size

  • Lifetime Qt Champion

    @Ralf said:
    try set widgets_i maximumheight to say 32 (for each)

  • Lifetime Qt Champion


    Your widgets are currently evenly spaced inside your layout. What you can do is add ui->verticalLayout->addStretch(1); after your loop. That will create a stretchable space that should push your widgets up as much as possible.

    Hope it helps

  • @mrjj nice approach, but then I have wasted space again, see red area

  • @SGaist added! The beginning of that layout is nice. The end again space waisted

  • Lifetime Qt Champion

    Are you expecting the layout contained in your "dock" widget to resize the widget itself ?

  • I added
    ui->verticalLayout->parentWidget()->setSizePolicy(QSizePolicy::Policy::Preferred, QSizePolicy::Policy::Maximum);
    which organized the containing widgets perfectly.
    But that opened another problem, No I can not resize (move border between dockwidgets) between the Dockwidgets..

Log in to reply