QTextEdit document()->size is invalid
-
As I edit a document, I check what the QTextEdit's text size is, because I want to adjust the containing widget's size to match the text height.
However I am finding that edit->document()->size() always has a bogus height e.g. 2 pixels.
What can be done to force QTextEdit to give an accurate text size?
-
Hi,
Can you provide a minimal compilable example that shows this behavior ?
-
You probably want to ask, "How do I get a line's height in QTextEdit?", because a document's height is usually measured by the number of lines, and not the height of its bounding rectangle, for example, or the sum of their heights.
You can test that to confirm, or just check the source code:
QSizeF QPlainTextDocumentLayout::documentSize() const { Q_D(const QPlainTextDocumentLayout); return QSizeF(d->maximumWidth, document()->lineCount()); }
So, you have to do that manually. Use
QTextBlock
's bounding rect or something similar. -
You probably want to ask, "How do I get a line's height in QTextEdit?", because a document's height is usually measured by the number of lines, and not the height of its bounding rectangle, for example, or the sum of their heights.
You can test that to confirm, or just check the source code:
QSizeF QPlainTextDocumentLayout::documentSize() const { Q_D(const QPlainTextDocumentLayout); return QSizeF(d->maximumWidth, document()->lineCount()); }
So, you have to do that manually. Use
QTextBlock
's bounding rect or something similar.