Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. Multi-line <PRE> in QTextEdit breaks line wrap
Forum Updated to NodeBB v4.3 + New Features

Multi-line <PRE> in QTextEdit breaks line wrap

Scheduled Pinned Locked Moved Unsolved Qt for Python
4 Posts 2 Posters 965 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    MostTornBrain
    wrote on last edited by
    #1

    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_())
    

    Screenshot 2023-09-17 at 3.27.57 PM.png

    M 1 Reply Last reply
    0
    • M MostTornBrain

      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_())
      

      Screenshot 2023-09-17 at 3.27.57 PM.png

      M Offline
      M Offline
      MostTornBrain
      wrote on last edited by
      #2

      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>
      
      M 1 Reply Last reply
      0
      • M MostTornBrain

        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>
        
        M Offline
        M Offline
        MostTornBrain
        wrote on last edited by MostTornBrain
        #3

        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)
        
        JonBJ 1 Reply Last reply
        0
        • M MostTornBrain

          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)
          
          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by JonB
          #4

          @MostTornBrain Well done for figuring a workaround. It would be good if you posted a link to your bug report.

          1 Reply Last reply
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved