Congratulations to our 2022 Qt Champions!

Fill all space in QLayout

  • Hello!!

    I have a custom widget that includes a QLabel and a QGraphicsView. I put this widgets in a layout by code but I don't know how to fill all the space available:


    I want this effect:


    This is my code:

    void GraphicsWindow::displayGraphics(int iNumCols) {
        int iRow = 0, iCol = 0;
        for (int iWidget = 0; iWidget < m_vectorWidgets.size(); iWidget++) {
            QWidget *widget =;
            widget->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
            ui->layoutCharts->addWidget(widget, iRow, iCol, Qt::AlignCenter);
            if (iCol == iNumCols) {       // iNumcols is the number of columns that I want
                iCol = 0;

    How can I fix it?

    Thank you very much!!

  • Moderators

    hi @ivanicy
    said in Fill all space in QLayout:


    I asume your custom widget, that contains the QLabel and QGraphicsView, is inside the m_vectorWidgets ?

    the code you showed seems good. So it can only be one of two things, that I can think of:

    • you have set a maxSize to the widget that is rather small the Layout will not resize its content beyond the maxsize of the items
    • QLabel and QGraphicsView are not inside a Layout themself and therefore do not get resized when the parent/container resizes

  • @J.Hilk

    I have put the widgets like the second image by default (when I run my program, they appear like the second image). To give functionality to the layout buttons, I put the widgets in this vector. But when I put again the widgets by code, they appear like the first image.

    I don't set any size to the widgets. I don't know what can I do.

  • Moderators

    @ivanicy very particular,

    I just tested it in a small example, remove Qt::AlignCenter and it should work.

    setting the alignment to Qt::AlignCenter seems to result in a disregard of the SizePolicy of the layout widgets.

  • @J.Hilk
    Wow! thank you very much!! That works fine!

Log in to reply