I'm developing a Qt Application and I'm trying to find a way to use QTextEdit as a label
with long text without the scroll bar. In my ui I have a QScrollArea
and inside of it I want to place a couple off QTextEdit
widgets and I only want use scrolling inside QScrollArea
. Problem is that no matter how I try to resize the QTextEdit
it seems it has a maximum height and cuts of text, even if I set the size manually and QTextEdit::size
returns the correct value.
I did the same thing with QLabel
and it works fine, but in this case I need some methods that are only provided in QTextEdit
.
I found this post: Resizing QT's QTextEdit to Match Text Height: maximumViewportSize()
And the answer given was the following:
I have solved this issue. There were 2 things that I had to do to get it to work:
- 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.
- 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.
The problem is that I have no idea how to access the QTextEdit's
QScrollArea
to enable widgetResizable
. Can anyone explain how I can achieve this or suggest a different way of resizing QTextEdit
to perfectly fit it's content?
This will allow the height of the text box to change as required. You can edit the code a little to handle the width as well.
connect( m_textField, SIGNAL( textChanged() ), this, SLOT( onTextChanged() ) );
void MyClass::onTextChanged()
{
QSize size = m_textField->document()->size().toSize();
m_textField->setFixedHeight( size.height() + 3 );
}