Uploaded image for project: 'Qt'
  1. Qt
  2. QTBUG-117161

Multi-line <PRE> in QTextEdit breaks line wrap

    XMLWordPrintable

Details

    • Bug
    • Resolution: Unresolved
    • P3: Somewhat important
    • None
    • 5.9.7, 5.15.2, 6.5.1
    • GUI: Text handling
    • None
    • macOS, Windows

    Description

      Using a multi-line <pre> tag in a QTextEdit is not handled correctly.  All text blocks after the <pre> tag is closed are rendered in no-wrap mode as if they were <pre> tagged.  If you then query the html using ::toHtml() you can see all blocks after the multi-line <pre> tag has closed are tagged as <pre> rather than <p>.

      This consistently happens with multi-line blocks given the <pre> formatting, either directly using the HTML <pre> tag or by assigning the "style="white-space: pre" to a <p> tag. 

      I am testing this using PyQt and I have tried on both MacOs and Windows using both Qt5 and Qt6 and the behavior is the same.

      Here is a simple program to demonstrate:

       

      from PyQt5.QtWidgets import QApplication, QTextEdit, QVBoxLayout, QWidget
      from PyQt5.QtGui import QColor, QTextOption
      import sys
      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 style=" white-space: pre; margin-top:0px; margin-bottom:12px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span style=" font-family:'Menlo';"> singe-line pre pre pre pre pre pre pre pre pre pre pre pre pre pre pre pre pre pre pre pre pre pre pre pre pre pre pre pre pre pre pre 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 multi-line pre multi-line pre multi-line pre multi-line pre multi-line pre multi-line pre multi-line pre</pre>''')
      widget.text_edit.append('''<p style="white-space: normal;"> Next lines of text should wrap normally normally normally normally normally normally normally normally normally normally normally normally </p>''')
      widget.text_edit.append('''<p style="white-space: normal;"> Next lines of text should wrap normally normally normally normally normally normally normally normally normally normally normally normally </p>''')
      print(widget.text_edit.toHtml())
      widget.show()
      sys.exit(app.exec())
      

      The end result looks like the attached file.  

      toHtml() shows the following is the resulting html created internally:

       

      <!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 pre pre pre pre pre pre pre pre pre pre pre pre pre pre pre pre pre pre pre pre pre pre pre pre pre pre pre pre pre pre 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';"> </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';">multi-line pre multi-line pre multi-line pre multi-line pre multi-line pre multi-line pre multi-line pre multi-line pre</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>
      <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>
      

       

      Attachments

        No reviews matched the request. Check your Options in the drop-down menu of this sections header.

        Activity

          People

            esabraha Eskil Abrahamsen Blomfeldt
            mosttornbrain Brian Stormont
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated:

              Gerrit Reviews

                There are no open Gerrit changes