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?
Thanks
-
@drmhkelley
Try to insert with the cursor:QTextCursor cursor=textEdit->textCursor(); cursor.movePosition(QTextCursor::End); cursor.insertHtml("string to append. ");
not tested ...
-
@drmhkelley
I can't reproduce what you described, Can you show us more code. -
@mpergand
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 asvoid log(QString msg)
{
moveCursor(QTextCursor::End, QTextCursor::MoveAnchor);
insertHtml(msg);
return;
}I get the described behavior with the following.
log("line 1");
log("line 2");
log("line 3");- list item
QString html = "<p>html line 1</p>"[link text](link url)
"<p>html line 2</p>"
"<p>html line 3</p>";
setHtml(true);
log(html);
setHtml(false);log("");
log("line 4");
log("line 5");
log("line 6");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.
Curious.
- list item
-
@drmhkelley
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. -
@drmhkelley said in Unexpected behavior in QTextEdit:
"<p>html line 2</p>"
"<p>html line 3</p>";try with <br> instead:
"html line 1<br>" -
@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.
-
@drmhkelley
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 futureline 4
etc. still had "paragraph gaps" where the earlierline 3
etc. 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.