Figuring out the minimum size of rich text
-
I have a QTextEdit which I initialize as follows:
QTextEdit* textEdit = new QTextEdit(); textEdit->setReadOnly(true); textEdit->setHtml("<p style=\"color:red; font-family:'Courier New';\">Raw Header</p>"); textEdit->setAlignment(Qt::AlignCenter); textEdit->append("<p style=\"font-family:'Courier New'\">00000000<br />00000000<br />00000000<br />00000000</p>"); textEdit->setAlignment(Qt::AlignCenter);
This textEdit is later added to a QGridLayout, and on that layout it looks like this:
I want to resize it to its minimum size without having scroll bars, something like:
textEdit->setFixedSize(minimumWidthWhileTextStillVisible, minimumHeightWhileTextStillVisible);
I tried to look around but I couldn't find a solution.
Every suggested solution I could find made the assumption that the QTextEdit is already visible which is not the case here.
I want to predetermine the width/height before theshow()
is called by the layout on that object. -
Good question. Maybe QTextDocument::size() returns it. Did not check it though.
-
I tried that, unfortunately
textEdit->document()->size()
returns (0,0), probably because the document is not visible yet...Will it help if I will subclass QTextEdit and re-implement the
sizeHint()
? Will the layout manager invoke thesizeHint()
after the object is visible? -
@Absurd said in Figuring out the minimum size of rich text:
Will it help if I will subclass QTextEdit and re-implement the sizeHint()?
How do you know the size then?
-
@Christian-Ehrlicher by returning
QTextDocument::size()
fromsizeHint()
.
If the QTextEdit is visible by the timesizeHint()
is invoked by the layout manager maybe it'll return the actual document size? -
Maybe first try out if the expected value of QTextDocument::size() is correct when the widget is visible. Then it's maybe worth a try.
-
@Christian-Ehrlicher thanks.
Yes, since posting that I tried that too, and that does not worth a try.
TheQTextDocument::size()
returns QSize(72, 173), so when I try to use that as the QTextEdit size I get:I was thinking iterating over the QTextBlock's in the QTextDocument and trying to figure out the expected size from QTextBlock::blockFormat() somehow, but again - if it's not visible everything returns 0 (for example QTextBlockFormat::lineHeight() returns 0 for the two blocks).
Is there a way to somehow render it internally into an object without actually
show()
'ing it? -
There is also a function QTextDocument::idealWidth() - what does it return?