title: Qt Resizing a QMainWindow with a Splitter
body: <p>I have a QMainWindow with:</p>
  
  <ul>
  <li>Two widgets in a horizontal splitter. "m_liner" is on the right side
  <ul>
  <li>Both widgets have a minimum size of say, 300 pixels.</li>
  </ul></li>
  <li>A checkbox to hide/show the right-side widget m_liner.</li>
  </ul>
  
  <p>I want the overall QMainWindow to <strong>expand</strong> when showing the widget, and <strong>shrink</strong> when hiding. The code below does this except:</p>
  
  <ul>
  <li>If both widgets are shown, the minimum window size is 600 pixels.</li>
  <li>Shrink the window to this smallest size.</li>
  <li>Uncheck the box to hide the right-side widget.</li>
  <li>Program hides the right-side widget.</li>
  <li>Program calls this->resize(300, height);</li>
  <li>The window ends up being 600 pixels wide (the minimum size with both widgets visible), instead of around 300 (the minimum size with only the left widget).</li>
  <li>Later, I can resize the window down to 300 pixels with the mouse or another button. But it won't resize to 300 in the checkbox event, even if I call resize several times.</li>
  </ul>
  
  <p>Does anyone have an idea how to solve this?</p>
  
  <p>Critical bit of code follows, I have a full project available if you need it:</p>
  
  <pre><code>void MainWindow::on_checkBox_stateChanged(int val)
  {
  std::cout &lt;&lt; "-------------------- Checkbox clicked "  &lt;&lt; val &lt;&lt; std::endl;
  bool visible = val;
  QWidget * m_liner = ui-&gt;textEdit_2;
  QSplitter * m_splitter = ui-&gt;splitter;
  
  int linerWidth = m_liner-&gt;width();
  if (linerWidth &lt;= 0) linerWidth = m_lastLinerWidth;
  if (linerWidth &lt;= 0) linerWidth = m_liner-&gt;sizeHint().width();
  // Account for the splitter handle
  linerWidth += m_splitter-&gt;handleWidth() - 4;
  
  std::cout &lt;&lt; "Frame width starts at " &lt;&lt; this-&gt;width() &lt;&lt; std::endl;
  std::cout &lt;&lt; "Right Panel width is " &lt;&lt; m_liner-&gt;width() &lt;&lt; std::endl;
  
  //  this-&gt;setUpdatesEnabled(false);
  if (visible &amp;&amp; !m_liner-&gt;isVisible())
  {
    // Expand the window to include the Right Panel
    int w = this-&gt;width() + linerWidth;
    m_liner-&gt;setVisible(true);
    QList&lt;int&gt; sizes = m_splitter-&gt;sizes();
    if (sizes[1] == 0)
    {
      sizes[1] = linerWidth;
      m_splitter-&gt;setSizes(sizes);
    }
    this-&gt;resize(w, this-&gt;height());
  }
  else if (!visible &amp;&amp; m_liner-&gt;isVisible())
  {
    // Shrink the window to exclude the Right Panel
    int w = this-&gt;width() - linerWidth;
    std::cout &lt;&lt; "Shrinking to " &lt;&lt; w &lt;&lt; std::endl;
    m_lastLinerWidth = m_liner-&gt;width();
    m_liner-&gt;setVisible(false);
    m_splitter-&gt;setStretchFactor(1, 0);
    this-&gt;resize(w, this-&gt;height());
    m_splitter-&gt;resize(w, this-&gt;height());
    this-&gt;update();
    this-&gt;resize(w, this-&gt;height());
  }
  else
  {
    // Toggle the visibility of the liner
    m_liner-&gt;setVisible(visible);
  }
  this-&gt;setUpdatesEnabled(true);
  std::cout &lt;&lt; "Frame width of " &lt;&lt; this-&gt;width() &lt;&lt; std::endl;
  }
  </code></pre>