Solved Set exact height for QPlainTextEdit (according to height)
-
I am trying to set the fixed height of a QPlainTextEdit to encompass exactly its text content, so that no vertical scrollbar is necessary and no space is wasted after the text.
I have until now failed to find a way to do this properly in code. I have tried several ways to do this, including :
QFontMetrics m(_ui->pendingCommand->fontMetrics()); int lineSpacing = m.lineSpacing(); _ui->pendingCommand->setFixedHeight(lineSpacing*_ui->pendingCommand->blockCount()*lineSpacing);
and
QFontMetrics m(_ui->pendingCommand->fontMetrics()); _ui->pendingCommand->setFixedHeight(m.boundingRect(_ui->pendingCommand->toPlainText()).height());
There are no contentMargins (all set to 0), and there is no horizontal scrollbar
I always have to add some "magic number" (like +8 pixels) to get this working as expected. This is nasty, and not robust to font changes.
How can I make the height of the widget match exactly the size of the text ?
-
@QuentinV said in Set exact height for QPlainTextEdit (according to height):
I always have to add some "magic number" (like +8 pixels) to get this working as expected. This is nasty, and not robust to font changes.
How can I make the height of the widget match exactly the size of the text ?The extra space comes from the QTextDocument's margin which by default is set to 4 pixels (=8px for top + bottom)
QAbstractTextDocumentLayout* docLayout = textEdit->document()->documentLayout(); const int docMargin = qCeil(doc->documentMargin());
Although something like this should work:
QMargins margins = textEdit->contentsMargins(); QSizeF documentSize = textEdit->document()->size(); int height = documentSize.height() + margins.top() + contentsMargins.bottom() + 1;
-
Thank you for your answer, but none of this work.
The contentMargins() property is equal to (0,0,0,0), and the documentSize's height is equal to the number of blocks in the text.
-
@QuentinV said in Set exact height for QPlainTextEdit (according to height):
Thank you for your answer, but none of this work.
i pointed out your "magic number"?
Simply add up all these properties (even if the contentMargins are 0) and you have the value you want in a pretty generic way. What you wanted in the first place? -
Well, it doesn't gives a height that fits exactly the text (which is my actual problem), and doesn't even add up to my magic number.
So yes, it generic, but it doesn't solves my problem.
-
@QuentinV
ok i now checked my code again, since i knew it should definitely work.
But it seems my code only works properly forQTextEdit
but not forQPlainTextEdit
. QPlainTextEdit returns a incorrect documentSize - which might be a bug IMO.This works correctly for QTextEdit:
QMargins margins = textEdit->contentsMargins(); QSizeF documentSize = textEdit->document()->documentLayout()->documentSize(); int height = documentSize.height() + margins.top() + margins.bottom() + 1;
Is switching to QTextEdit an option for you?
-
We have no strong performance issues, so we will switch to QTextEdit. Thank you for your help.
I am new to Qt, how can I file a bug to report the problem of QPlainTextEdit ?
-
@QuentinV said in Set exact height for QPlainTextEdit (according to height):
I am new to Qt, how can I file a bug to report the problem of QPlainTextEdit ?
- https://bugreports.qt.io
- login with your forum account
- press "create" button in the top toolbar
-
@raven-worx
Well, it is apparently not a bug. It has already been reported, and the QPlainTextDocumentLayout returns by design the number of blocks for its height.