Multi-line <PRE> in QTextEdit breaks line wrap
-
Hi,
I'm using a QTextEdit to display rich text, however I have run across an oddity I can not work-around. I have the widget configured to automatically wrap lines at the widget border, and this works fine until I display a multi-line <PRE> tagged text. I realize multi-line <PRE> shouldn't wrap, however what happens is everything after this <PRE> tag is closed also no longer auto line wraps.
Is there some way to prevent this from happening, or restore the auto-wrap behavior after a multi-line <PRE> is displayed?
Here's a simple example demonstrating how a single line <pre> is fine, but a multi-line pre breaks line wrap:
class CustomWidget(QWidget): def __init__(self, *args, **kwargs): super(CustomWidget, self).__init__(*args, **kwargs) self.layout = QVBoxLayout() self.text_edit = QTextEdit() self.layout.addWidget(self.text_edit) self.setLayout(self.layout) app = QApplication(sys.argv) widget = CustomWidget() widget.text_edit.append('''<p> Next lines of text should wrap normally normally normally normally normally normally normally normally normally normally normally normally </p>''') widget.text_edit.append('''<pre> singe-line pre </pre>''') widget.text_edit.append('''<p> Next lines of text should wrap normally normally normally normally normally normally normally normally normally normally normally normally </p>''') widget.text_edit.append('''<pre> multi-line pre </pre>''') widget.text_edit.append('''<p> Next lines of text should wrap normally normally normally normally normally normally normally normally normally normally normally normally </p>''') widget.show() sys.exit(app.exec_())
-
Some additional information I discovered: If I use the QTextEdit toHtml() function to view what it has rendered, it appears the widget is stuck in a quasi <pre> state. Even though I added a new <p> tagged text, toHtml() shows it has a <pre> tag instead, which would explain the lack of line wrapping. Oddly, the actual <pre> multi-line text is rendered as a <p> tag. Seems like some internal <pre> state is getting misapplied to the text blocks. Rather than make the real <pre> tagged text pre formatted, it instead makes all subsequent blocks pre formatted, but assigns the fonts correctly.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN" "http://www.w3.org/TR/REC-html40/strict.dtd"> <html><head><meta name="qrichtext" content="1" /><style type="text/css"> p, li { white-space: pre-wrap; } </style></head><body style=" font-family:'.AppleSystemUIFont'; font-size:13pt; font-weight:400; font-style:normal;"> <p style=" margin-top:12px; margin-bottom:12px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;">Next lines of text should wrap normally normally normally normally normally normally normally normally normally normally normally normally </p> <p style=" margin-top:12px; margin-bottom:12px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span style=" font-family:'Menlo';"> singe-line pre </span></p> <p style=" margin-top:12px; margin-bottom:12px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;">Next lines of text should wrap normally normally normally normally normally normally normally normally normally normally normally normally </p> <p style=" margin-top:12px; margin-bottom:12px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span style=" font-family:'Menlo';"> multi-line pre </span></p> <pre style=" margin-top:0px; margin-bottom:12px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span style=" font-family:'Menlo';"> </span></pre> <pre style=" margin-top:0px; margin-bottom:12px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;">Next lines of text should wrap normally normally normally normally normally normally normally normally normally normally normally normally </pre></body></html>
-
I've filed a bug about this on the Qt Bug Tracker Jira system, because I'm fairly certain it is a bug.
On the bright-side, I did figure out a work-around. It looks like the QTextEdit is getting stuck in a mode where all future TextBlocks end up with the format property of setNonBreakableLines(True). To work-around this, any time I send a multi-line <pre> text block to QTextEdit, I immediately follow it up with a call to this custom function:
def restoreLineWrap(self): cursor = self.textCursor() format = QTextBlockFormat() format.setNonBreakableLines(False) cursor.setBlockFormat(format) self.setTextCursor(cursor)
-
@MostTornBrain Well done for figuring a workaround. It would be good if you posted a link to your bug report.