Unsolved Unexpected behavior in QTextEdit
I routinely use a QTextEdit-based logging widget for diagnostic or other output from any app I'm developing. Up until now, that had been used exclusively for plaintext data. However, I had recent occasion to dump some richtext formatted output to the QTextEditor, using insertHtml(). That worked mostly just fine. However, that flips the QTextEdit widget from "single-spaced" to "double-spaced" output. I haven't found anything that discusses control of anything like "line spacing" in a QTextEdit widget or its underlying QDocument object.
Any suggestion why this happens and what I might do to reverse the behavior?
Try to insert with the cursor:
QTextCursor cursor=textEdit->textCursor(); cursor.movePosition(QTextCursor::End); cursor.insertHtml("string to append. ");
not tested ...
@mpergand That is precisely how I do it.
I can't reproduce what you described, Can you show us more code.
The code is very simple. I have logging widget, pMainOut, that just extends a QTextEdit widget a bit. In particular, it adds method log, which is essentially defined as
void log(QString msg)
I get the described behavior with the following.
- list item
QString html = "<p>html line 1</p>"[link text](link url)
"<p>html line 2</p>"
"<p>html line 3</p>";
I have attempted to attach a screen shot showing that the first three lines are single-spaced, but the remaining ones double-spaces. Incidentally, when I cut/past the contents of the output window as plaintext, there no extre linefeeds. The double spacing is not from extraneous blank lines, but just single lines display as double-spaced.
- list item
Sorry - I was a bit sloppy with that. And I left out an important point for the "log" method. It has a switch that depends on whether the logged text is plaintext or html. If html, then it uses insertHtml() as shown. If plaintext, it uses insertPlaintext() instead.
@mpergand Thanks for that!! I can't begin to understand how or why that works, but it removed the symptom.
However, my main take-away is that at least one (and likely more) html tag has unexpected effects on the QTextEdit widget. I'll wait to see how it behaves with a variety of more complicated html outputs to declare this a solved issue.
jeremy_k last edited by
QTextEdit converts the inserted html and plain text. The spacing is due to the style that is automatically applied unless the inserted html already specifies styling. Have a look at the output of QTextEdit::toHtml().
JonB last edited by JonB
I don't understand. Naturally where you used
<p>...</p>you got "extra" spacing as these are HTML paragraphs, so you get a blank line afterwards. (You could verify it is not line spacing by making your text in each case much longer so it spills across multiple lines.)
That was never the problem. The issue was that your output shows that after the last
<p>html line 3</p>your future
line 4etc. still had "paragraph gaps" where the earlier
line 3etc. did not.
There is some indication from your output & code that once you have put in a
<p>...</p>the text edit gets mixed up from then onwards.