title: Resizing QT's QTextEdit to Match Text Height: maximumViewportSize()
body: <p>I am trying to use a QTextEdit widget inside of a form containing several QT widgets.  The form itself sits inside a QScrollArea that is the central widget for a window.  My intent is that any necessary scrolling will take place in the main QScrollArea (rather than inside any widgets), and any widgets inside will automatically resize their height to hold their contents.</p>
  
  <p>I have tried to implement the automatic resizing of height with a QTextEdit, but have run into an odd issue.  I created a sub-class of QTextEdit and reimplemented sizeHint() like this:</p>
  
  <pre><code>QSize OperationEditor::sizeHint() const {
    QSize sizehint = QTextBrowser::sizeHint();
    sizehint.setHeight(this-&gt;fitted_height);
    return sizehint;
  }
  </code></pre>
  
  <p>this->fitted_height is kept up-to-date via this slot that is wired to the QTextEdit's "contentsChanged()" signal:</p>
  
  <pre><code>void OperationEditor::fitHeightToDocument() {
        this-&gt;document()-&gt;setTextWidth(this-&gt;viewport()-&gt;width());
        QSize document_size(this-&gt;document()-&gt;size().toSize());
        this-&gt;fitted_height = document_size.height();
        this-&gt;updateGeometry();
  }
  </code></pre>
  
  <p>The size policy of the QTextEdit sub-class is:</p>
  
  <pre><code>this-&gt;setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::Preferred);
  </code></pre>
  
  <p>I took this approach after reading <a href="https://stackoverflow.com/questions/1153714/pyqt-how-to-handle-auto-resize-of-widgets-when-their-content-changes">this post.</a></p>
  
  <p>Here is my problem:</p>
  
  <p>As the QTextEdit gradually resizes to fill the window, it stops getting larger and starts scrolling within the QTextEdit, no matter what height is returned from sizeHint().  If I initially have sizeHint() return some large constant number, then the QTextEdit is very big and is contained nicely within the outer QScrollArea, as one would expect.  However, if sizeHint gradually adjusts the size of the QTextEdit rather than just making it really big to start, then it tops out when it fills the current window and starts scrolling instead of growing.</p>
  
  <p>I have traced this problem to be that, no matter what my sizeHint() returns, it will never resize the QTextEdit larger than the value returned from maximumViewportSize(), which is inherited from QAbstractScrollArea.  Note that this is not the same number as viewport()->maximumSize().  I am unable to figure out how to set that value.  </p>
  
  <p>Looking at QT's source code, maximumViewportSize() is returning "the size of the viewport as if the scroll bars had no valid scrolling range."  This value is basically computed as the current size of the widget minus (2 * frameWidth + margins) plus any scrollbar widths/heights. This does not make a lot of sense to me, and it's not clear to me why that number would be used anywhere in a way that supercede's the sub-class's sizeHint() implementation.  Also, it does seem odd that the single "frameWidth" integer is used in computing both the width and the height.</p>
  
  <p>Can anyone please shed some light on this?  I suspect that my poor understanding of QT's layout engine is to blame here.</p>
  
  <p>Edit: after initially posting this, I had the idea to reimplement maximumViewportSize() to return the same thing as sizeHint().  Unfortunately, this did not work as I still have the same problem.</p>