0
votes

I am trying to create an expandable Qt dialog application. The main layout is a QVBoxLayout. The top part has two views and a QPushButtonbutton. Clicking button will unfold the bottom widget which is initially hidden. In the bottom widget, there is another push button, which could fold (hide) the bottom widget. When the bottom widget fold/unfold, I expect the size of the dialog size to change as well.

But for some reason, the dialog size only increases when the bottom widget is unfolded. And never shrink back to (200, 100). Is there anything I missed?

Environment: Qt Creator 3.6.1; Based on Qt5.6.0 (MSVC2013 32bit); build on Mar 14 2016; revision d502727b2c

The code I am using :

Dialog::Dialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::Dialog)
{
    ui->setupUi(this);

    QTreeView *tree = new QTreeView;
    QTableView *table = new QTableView;
    QPushButton *button_show = new QPushButton;
    button_show->setText(tr("Show hidden panel"));
    QHBoxLayout *layout_top = new QHBoxLayout;
    layout_top->addWidget(tree);
    layout_top->addWidget(table);
    layout_top->addWidget(button_show);


    QHBoxLayout *layout_bottom = new QHBoxLayout;
    QTextEdit *editor = new QTextEdit;
    QPushButton *button_hide = new QPushButton;
    button_hide->setText(tr("Hide the bottom panel"));

    g_pEditor = editor;
    layout_bottom->addWidget(editor);
    layout_bottom->addWidget(button_hide);
    QWidget *panel = new QWidget;
    panel->setLayout(layout_bottom);

    QVBoxLayout *layout_main = new QVBoxLayout;
    layout_main->addLayout(layout_top);
    layout_main->addWidget(panel);
    setLayout(layout_main);

  panel->hide();
  connect(button_show, &QPushButton::clicked
      , panel
      , [=]()
  {
    panel->setVisible(true);
    button_show->setEnabled(false);
    resize(200, 200);// not really working, the dialog size is able to increase without calling resize()
  });

  connect(button_hide, &QPushButton::clicked, panel, [=]()
  {
    panel->hide();
    button_show->setEnabled(true);
    resize(200,100);// does not shrink the dialog size*
  });

  resize(200,100);
}

Thanks for your help :)

1
Should it be possible to manually resize the dialog? If not you could set the size constraint of the main layout to QLayout::SetFixedSize. The layout should then automatically resize when you hide or show widgets.A.Fagrell

1 Answers

1
votes

Your should try setFixedSize(w, h) instead. This sets both, the minimum and the maximum size to (w, h). "This will override the default size constraints set by QLayout."