title: Qt layouts - Keep widget aspect ratio while resizing
body: <p>I want to keep an aspect ratio of 16/9 on a QGroupBox (the one on the left on the screenshot below). For the tests I made a simple program with 4 groupboxes in a vertical layout, all in an horizontal layout:</p>
  
  <p><img src="https://i.stack.imgur.com/GqU2y.png" alt="">
  <strong>main.cpp</strong></p>
  
  <pre><code>#include "MainWindow.h"
  #include &lt;QApplication&gt;
  
  int main(int argc, char *argv[])
  {
      QApplication a(argc, argv);
      MainWindow w;
      w.show();
  
      return a.exec();
  }
  </code></pre>
  
  <p><strong>MainWindow.h</strong></p>
  
  <pre><code>#ifndef MAINWINDOW_H
  #define MAINWINDOW_H
  
  #include &lt;QMainWindow&gt;
  
  namespace Ui {
  class MainWindow;
  }
  
  class MainWindow : public QMainWindow
  {
      Q_OBJECT
  
  public:
      explicit MainWindow(QWidget *parent = 0);
      ~MainWindow();
  
  
  private:
      Ui::MainWindow *ui;
  };
  
  #endif // MAINWINDOW_H
  </code></pre>
  
  <p><strong>MainWindow.cpp</strong></p>
  
  <pre><code>#include "MainWindow.h"
  #include "ui_MainWindow.h"
  
  #include &lt;QDebug&gt;
  
  MainWindow::MainWindow(QWidget *parent) :
      QMainWindow(parent),
      ui(new Ui::MainWindow)
  {
      ui-&gt;setupUi(this);
      qDebug() &lt;&lt; "horizontalLayout-&gt;hasHeightForWidth: " &lt;&lt; ui-&gt;horizontalLayout-&gt;hasHeightForWidth();
  }
  
  MainWindow::~MainWindow()
  {
      delete ui;
  }
  </code></pre>
  
  <p><strong>MainWindow.ui</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;ui version="4.0"&gt;
   &lt;class&gt;MainWindow&lt;/class&gt;
   &lt;widget class="QMainWindow" name="MainWindow"&gt;
    &lt;property name="geometry"&gt;
     &lt;rect&gt;
      &lt;x&gt;0&lt;/x&gt;
      &lt;y&gt;0&lt;/y&gt;
      &lt;width&gt;1000&lt;/width&gt;
      &lt;height&gt;703&lt;/height&gt;
     &lt;/rect&gt;
    &lt;/property&gt;
    &lt;property name="windowTitle"&gt;
     &lt;string&gt;MainWindow&lt;/string&gt;
    &lt;/property&gt;
    &lt;widget class="QWidget" name="centralWidget"&gt;
     &lt;layout class="QHBoxLayout" name="horizontalLayout"&gt;
      &lt;item&gt;
       &lt;widget class="GroupBoxHFW" name="groupBox_1"&gt;
        &lt;property name="title"&gt;
         &lt;string&gt;GroupBox&lt;/string&gt;
        &lt;/property&gt;
       &lt;/widget&gt;
      &lt;/item&gt;
      &lt;item&gt;
       &lt;layout class="QVBoxLayout" name="verticalLayout"&gt;
        &lt;item&gt;
         &lt;widget class="QGroupBox" name="groupBox_2"&gt;
          &lt;property name="minimumSize"&gt;
           &lt;size&gt;
            &lt;width&gt;485&lt;/width&gt;
            &lt;height&gt;204&lt;/height&gt;
           &lt;/size&gt;
          &lt;/property&gt;
          &lt;property name="maximumSize"&gt;
           &lt;size&gt;
            &lt;width&gt;485&lt;/width&gt;
            &lt;height&gt;204&lt;/height&gt;
           &lt;/size&gt;
          &lt;/property&gt;
          &lt;property name="title"&gt;
           &lt;string&gt;GroupBox&lt;/string&gt;
          &lt;/property&gt;
         &lt;/widget&gt;
        &lt;/item&gt;
        &lt;item&gt;
         &lt;widget class="QGroupBox" name="groupBox_3"&gt;
          &lt;property name="minimumSize"&gt;
           &lt;size&gt;
            &lt;width&gt;485&lt;/width&gt;
            &lt;height&gt;204&lt;/height&gt;
           &lt;/size&gt;
          &lt;/property&gt;
          &lt;property name="maximumSize"&gt;
           &lt;size&gt;
            &lt;width&gt;485&lt;/width&gt;
            &lt;height&gt;204&lt;/height&gt;
           &lt;/size&gt;
          &lt;/property&gt;
          &lt;property name="title"&gt;
           &lt;string&gt;GroupBox&lt;/string&gt;
          &lt;/property&gt;
         &lt;/widget&gt;
        &lt;/item&gt;
        &lt;item&gt;
         &lt;widget class="QGroupBox" name="groupBox_4"&gt;
          &lt;property name="minimumSize"&gt;
           &lt;size&gt;
            &lt;width&gt;485&lt;/width&gt;
            &lt;height&gt;204&lt;/height&gt;
           &lt;/size&gt;
          &lt;/property&gt;
          &lt;property name="maximumSize"&gt;
           &lt;size&gt;
            &lt;width&gt;485&lt;/width&gt;
            &lt;height&gt;204&lt;/height&gt;
           &lt;/size&gt;
          &lt;/property&gt;
          &lt;property name="title"&gt;
           &lt;string&gt;GroupBox&lt;/string&gt;
          &lt;/property&gt;
         &lt;/widget&gt;
        &lt;/item&gt;
        &lt;item&gt;
         &lt;spacer name="verticalSpacer"&gt;
          &lt;property name="orientation"&gt;
           &lt;enum&gt;Qt::Vertical&lt;/enum&gt;
          &lt;/property&gt;
          &lt;property name="sizeHint" stdset="0"&gt;
           &lt;size&gt;
            &lt;width&gt;20&lt;/width&gt;
            &lt;height&gt;40&lt;/height&gt;
           &lt;/size&gt;
          &lt;/property&gt;
         &lt;/spacer&gt;
        &lt;/item&gt;
       &lt;/layout&gt;
      &lt;/item&gt;
     &lt;/layout&gt;
    &lt;/widget&gt;
    &lt;widget class="QMenuBar" name="menuBar"&gt;
     &lt;property name="geometry"&gt;
      &lt;rect&gt;
       &lt;x&gt;0&lt;/x&gt;
       &lt;y&gt;0&lt;/y&gt;
       &lt;width&gt;1000&lt;/width&gt;
       &lt;height&gt;21&lt;/height&gt;
      &lt;/rect&gt;
     &lt;/property&gt;
    &lt;/widget&gt;
    &lt;widget class="QToolBar" name="toolBar"&gt;
     &lt;property name="windowTitle"&gt;
      &lt;string&gt;toolBar&lt;/string&gt;
     &lt;/property&gt;
     &lt;attribute name="toolBarArea"&gt;
      &lt;enum&gt;TopToolBarArea&lt;/enum&gt;
     &lt;/attribute&gt;
     &lt;attribute name="toolBarBreak"&gt;
      &lt;bool&gt;false&lt;/bool&gt;
     &lt;/attribute&gt;
    &lt;/widget&gt;
    &lt;widget class="QStatusBar" name="statusBar"/&gt;
   &lt;/widget&gt;
   &lt;layoutdefault spacing="6" margin="11"/&gt;
   &lt;customwidgets&gt;
    &lt;customwidget&gt;
     &lt;class&gt;GroupBoxHFW&lt;/class&gt;
     &lt;extends&gt;QGroupBox&lt;/extends&gt;
     &lt;header&gt;GroupBoxHFW.h&lt;/header&gt;
     &lt;container&gt;1&lt;/container&gt;
    &lt;/customwidget&gt;
   &lt;/customwidgets&gt;
   &lt;resources/&gt;
   &lt;connections/&gt;
  &lt;/ui&gt;
  </code></pre>
  
  <p><strong>GroupBoxHFW.h</strong></p>
  
  <pre><code>#ifndef GROUPBOXHFW_H
  #define GROUPBOXHFW_H
  
  #include &lt;QGroupBox&gt;
  
  class GroupBoxHFW : public QGroupBox
  {
  public:
      GroupBoxHFW(QWidget * parent = 0);
      GroupBoxHFW(const QString &amp; title, QWidget * parent = 0);
  
      int heightForWidth(int w) const;
      bool hasHeightForWidth() const;
  };
  
  #endif // GROUPBOXHFW_H
  </code></pre>
  
  <p><strong>GroupBoxHFW.cpp</strong></p>
  
  <pre><code>#include "GroupBoxHFW.h"
  
  #include &lt;QDebug&gt;
  
  GroupBoxHFW::GroupBoxHFW(QWidget * parent) : QGroupBox(parent)
  {
      QSizePolicy policy(this-&gt;sizePolicy());
      policy.setHeightForWidth(true);
  
      this-&gt;setSizePolicy(policy);
  }
  
  GroupBoxHFW::GroupBoxHFW(const QString &amp; title, QWidget * parent) : QGroupBox(title, parent)
  {
      QSizePolicy policy(this-&gt;sizePolicy());
      policy.setHeightForWidth(true);
  
      this-&gt;setSizePolicy(policy);
  }
  
  
  int GroupBoxHFW::heightForWidth(int w) const
  {
      qDebug() &lt;&lt; "GroupBoxHFW::heightForWidth called";
      return 9.f / 16.f * w;
  }
  
  bool GroupBoxHFW::hasHeightForWidth() const
  {
      qDebug() &lt;&lt; "GroupBoxHFW::hasHeightForWidth called";
      return true;
  }
  </code></pre>
  
  <p>As you can see, I reimplemented the virtual functions <code>int heightForWidth(int w) const</code> and <code>bool hasHeightForWidth() const</code>, hoping my QGroupBox would resize automatically to a 16/9 aspect ratio. Unfortunately, it doesn't work. <code>GroupBoxHFW::heightForWidth()</code> is called only once when the program starts and it doesn't even resize the widget.</p>
  
  <p>Here is the output of the program:</p>
  
  <pre><code>GroupBoxHFW::hasHeightForWidth called
  horizontalLayout-&gt;hasHeightForWidth:  true
  GroupBoxHFW::hasHeightForWidth called
  GroupBoxHFW::heightForWidth called
  GroupBoxHFW::hasHeightForWidth called
  GroupBoxHFW::hasHeightForWidth called
  GroupBoxHFW::hasHeightForWidth called
  GroupBoxHFW::hasHeightForWidth called
  </code></pre>
  
  <p>What's even weirder is that none of the functions above (<code>hasHeightForWidth()</code> and <code>heightForWidth()</code> get called when I resize the window, while the QGroupBox is resized.</p>
  
  <p>What's wrong?</p>