Important: Please read the Qt Code of Conduct -

How force a widget to fill the empty space of another hidden widget?

  • Hi,

    I work on a application where it's GUI looks this:

    | 1 | 2 |

    | 3 | 4 |

    All the 4 widgets are shown (not hidden) at the beginning. It has a menu with view options where the user can hide widget number 2, 3 and 4.

    I want the widget number one stretch and use the empty place of the other hidden widgets. The size policy is expanding for both vertical and horizontal. I have changed the stretch horizontal and vertical to 100 but it did not work. Also, there is no limitation in maximum size of the widgets.

    I should mention that the whole layout is in Grid format.

    I would be thankful if someone help me :)

  • Lifetime Qt Champion


    What exact layout are you using ?

    Grids can be achieved using QGridLayout but also a combination of QVBoxLayout and QHBoxLayout.

  • @SGaist I used the default grid system of Qt creator for the Main Window. In the centeralWdiget, the layout name is gridLayout. So, I think it is the the QGridLayout.

  • Lifetime Qt Champion

    In that case you have to change the span of widget 1 when hiding widget 2. This likely means that you have to pull widget one of the grid and put it back in with the appropriate span value.

  • @SGaist I can use resize() only when there is no Layout. When I set the grid layout the resize does not work. When there are only two vertical widgets (in my example 1 and 3), it works perfectly.

    But, with all the four widgets:

    • If I only hide number 3, number 1 does NOT extend.
    • If I hide both number 3 and number 4 then number 1 and 2 extend and fill the empty place bellow them.

    It looks the grid system wants 1 and 2 be have same height.

  • Moderators

    @Bamshad there are a couple of ways to do this, Like @SGaist said, is one.

    If you don't force stretch factors and set the size policy to expanding, then you don't have to do anything. Like in this small example:

    int main(int argc, char *argv[])
        QApplication a(argc, argv);
        QWidget w;
        QVBoxLayout *vLay = new QVBoxLayout(&w);
        QPushButton *btn = new QPushButton("Hide/Show");
        QGridLayout *gLay = new QGridLayout;
        QLabel *l = new QLabel("Widget 1");
        l->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
        l = new QLabel("Widget 2");
        l->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
        QObject::connect(btn, &QPushButton::clicked, l, [l]()->void{l->setVisible(!l->isVisible());});
        l = new QLabel("Widget 3");
        l->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
        QObject::connect(btn, &QPushButton::clicked, l, [l]()->void{l->setVisible(!l->isVisible());});
        l = new QLabel("Widget 4");
        l->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
        QObject::connect(btn, &QPushButton::clicked, l, [l]()->void{l->setVisible(!l->isVisible());});
        return  a.exec();

  • Lifetime Qt Champion

    I didn't mention resize at any point. I wrote about the span values that you can use to make widgets use more than one column/row.

  • @SGaist So, how can I change the span value? the widget is a scroll area.

  • Lifetime Qt Champion

    I already suggested a possible way to implement that, didn't I ?

  • @SGaist Is it possible to see the columns and rows in the Design?

  • Lifetime Qt Champion

    You'll see them once you add the widgets where you want them.

Log in to reply