I've noticed that the minimum size hint of widgets in a layout are ignored if you add the widgets later on (i.e. not in the constructor).

Here is an example that reproduces the problem I'm having.

layoutproblem.h
Qt Code:
  1. #ifndef LAYOUTPROBLEM_H
  2. #define LAYOUTPROBLEM_H
  3.  
  4. #include <QWidget>
  5.  
  6. class QMdiArea;
  7.  
  8. class LayoutProblem : public QWidget
  9. {
  10. Q_OBJECT
  11.  
  12. public:
  13. explicit LayoutProblem(QWidget *parent = 0);
  14. virtual ~LayoutProblem();
  15.  
  16. private slots:
  17. void createUI();
  18.  
  19. private:
  20. QMdiArea* mdiArea_;
  21. };
  22.  
  23. #endif // LAYOUTPROBLEM_H
To copy to clipboard, switch view to plain text mode 

layoutproblem.cpp
Qt Code:
  1. #include "layoutproblem.h"
  2.  
  3. #include <QVBoxLayout>
  4. #include <QGridLayout>
  5. #include <QMdiArea>
  6. #include <QTimer>
  7. #include <QTabWidget>
  8. #include <QApplication>
  9. #include <QPushButton>
  10.  
  11.  
  12. LayoutProblem::LayoutProblem(QWidget *parent) :
  13. QWidget(parent),
  14. mdiArea_(new QMdiArea)
  15. {
  16. setLayout(new QGridLayout);
  17. layout()->addWidget(mdiArea_);
  18. mdiArea_->setViewMode(QMdiArea::TabbedView);
  19.  
  20. resize(200, 200);
  21.  
  22. QTimer::singleShot(500, this, SLOT(createUI()));
  23. // createUI();
  24. }
  25.  
  26. LayoutProblem::~LayoutProblem()
  27. {
  28. }
  29.  
  30. void LayoutProblem::createUI()
  31. {
  32. for(unsigned i = 0; i != 2; ++i)
  33. {
  34. QWidget* window = new QWidget;
  35. window->setLayout(new QVBoxLayout);
  36. QPushButton* w = new QPushButton("test");
  37. w->setMinimumSize(200, 200);
  38. w->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::MinimumExpanding);
  39. window->layout()->addWidget(w);
  40.  
  41. w = new QPushButton("test");
  42. w->setMinimumSize(200, 200);
  43. w->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::MinimumExpanding);
  44. window->layout()->addWidget(w);
  45.  
  46. QTabWidget* tab = new QTabWidget;
  47. tab->addTab(window, "tab " + QString::number(i));
  48.  
  49. mdiArea_->addSubWindow(tab);
  50. tab->setWindowTitle("window " + QString::number(i));
  51. tab->showMaximized();
  52. }
  53. }
  54.  
  55. int main(int argc, char *argv[])
  56. {
  57. QApplication a(argc, argv);
  58. LayoutProblem w;
  59. w.show();
  60.  
  61. return a.exec();
  62. }
To copy to clipboard, switch view to plain text mode 

Run this code and you will notice that you can scale the window much smaller than the minimum size hint of the QPushButton (which is set to 200x200). Next, try commenting the QTimer line and call createUI() directly:

Qt Code:
  1. // QTimer::singleShot(500, this, SLOT(createUI()));
  2. createUI();
To copy to clipboard, switch view to plain text mode 

Re-run the code and now everything works as expected.

How can I add widgets to my layouts dynamically without Qt ignoring the minimum size hints?