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

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:

    0_1547637823265_e37730ab-40d0-4e21-8195-57bfbc0bf196-image.png

    I want this effect:

    0_1547638009053_f88cda39-637c-477b-87b9-340dfea58c05-image.png

    This is my code:

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

    How can I fix it?

    Thank you very much!!


  • Moderators

    hi @ivanicy
    said in Fill all space in QLayout:

    m_vectorWidgets

    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