Resizing qt widgets when their children are hidden

The adjustSize function may do what you want.


Here is a basic example which does auto-resize upon widget hide/show.

dialog.h file:

#ifndef DIALOG_H
#define DIALOG_H

#include <QDialog>
#include <QtGui>

class dialog : public QDialog
{
    Q_OBJECT


public:
    explicit dialog(QWidget *parent = 0)
    {

        vlayout.addWidget(&checkbox);
        vlayout.addWidget(&label);
        label.setText("Label");

        setLayout(&vlayout);
        this->layout()->setSizeConstraint(QLayout::SetFixedSize); // !!! This is the what makes it auto-resize

        checkbox.setChecked(true);
        connect(&checkbox,SIGNAL(toggled(bool)),&label,SLOT(setVisible(bool)));
    }

private:
    QVBoxLayout vlayout;
    QCheckBox checkbox;
    QLabel label;

};

#endif // DIALOG_H

and main.c

#include <QApplication>
#include "dialog.h"

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    dialog d;
    d.show();
    return a.exec();
}

Tags:

Qt

Resize

Qwidget