Resizing QT's QTextEdit to Match Text Height: maximumViewportSize()
Asked Answered
B

2

14

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.

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:

QSize OperationEditor::sizeHint() const {
  QSize sizehint = QTextBrowser::sizeHint();
  sizehint.setHeight(this->fitted_height);
  return sizehint;
}

this->fitted_height is kept up-to-date via this slot that is wired to the QTextEdit's "contentsChanged()" signal:

void OperationEditor::fitHeightToDocument() {
      this->document()->setTextWidth(this->viewport()->width());
      QSize document_size(this->document()->size().toSize());
      this->fitted_height = document_size.height();
      this->updateGeometry();
}

The size policy of the QTextEdit sub-class is:

this->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::Preferred);

I took this approach after reading this post.

Here is my problem:

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.

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.

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.

Can anyone please shed some light on this? I suspect that my poor understanding of QT's layout engine is to blame here.

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.

Boris answered 16/6, 2010 at 3:26 Comment(0)
B
12

I have solved this issue. There were 2 things that I had to do to get it to work:

  1. Walk up the widget hierarchy and make sure all the size policies made sense to ensure that if any child widget wanted to be big/small, then the parent widget would want to be the same thing.

  2. This is the main source of the fix. It turns out that since the QTextEdit is inside a QFrame that is the main widget in a QScrollArea, the QScrollArea has a constraint that it will not resize the internal widget unless the "widgetResizable" property is true. The documentation for that is here: http://doc.qt.io/qt-4.8/qscrollarea.html#widgetResizable-prop. The documentation was not clear to me until I played around with this setting and got it to work. From the docs, it seems that this property only deals with times where the main scroll area wants to resize a widget (i.e. from parent to child). It actually means that if the main widget in the scroll area wants to ever resize (i.e. child to parent), then this setting has to be set to true.

So, the moral of the story is that the QTextEdit code was correct in overriding sizeHint, but the QScrollArea was ignoring the value returned from the main frame's sizeHint.

Yay! It Works!

Boris answered 19/6, 2010 at 19:13 Comment(7)
Thanks for finding this one out. It will definitely help me later one.Nika
Aaron, could elaborate on your solution? I can't seem to access a QScrollArea associated with a QTextEdit. Can you explain, given a QTextEdit what you set the widgetResizable property on? i.e. how they're connected? QTextEdit actually derives from QAbstractScrollArea which doesn't have a setWidgetResizable property (I'm in Qt 4.7). Thanks this is really bothering me!!!Rotgut
@supertwang you might want to check my answer to a similar question here.Nonfeasance
I also had similar problem, but with QPlainTextEdit, so if anybody has problems with QPlainTextEdit - be aware, that it has different document layout manager and it reports document()->size() in number of lines, not in number of pixels. You have to get QFont from the widget, use QFontMetrics::lineSpacing() and multiply it by document()->size() to get height in pixels. You can ready about it in the general description of QPlainTextEdit class.Ask
Sounds promising, without an example is hard to see how to implement this. Because of Dave's question.Brady
@Rotgut did you ever managed to do it? I have the same problem right now.Coryphaeus
For me, it was just setting QScrollArea to setWidgetResizable(true);. Thanks so much!Knurled
N
0

You may try setting minimumSize property of the QTextEdit to see if that force the layout to grow.

I don't understand most of Qt's layout scheme but setting minimum and maximum size pretty much does what I want it to do. Well, most of the time anyways.

Nika answered 16/6, 2010 at 17:5 Comment(2)
Thanks for the answer, Stephen. I have tried setting every combination of minimumSize, maximumSize, and fixedSize (with the corresponding QSizePolicy's), but none have made any difference.Boris
agreed, in my case I found that I have to set both minimum and maximum sizes for it to properly work.Singles

© 2022 - 2024 — McMap. All rights reserved.