<div dir="ltr"><div>Windows 11 / Python 3.11 / PyQt 6.5.0</div><div><br></div><div>I believe I've discovered a bug in the QTextEdit of PyQT6, specifically how it exports the current text via toHTML() or toMarkdown() depending on how text is added to the QTextEdit. Here is some sample code, as simplified as I could manage it.</div><div><br></div><div>First, let's look at some code that works as expected:</div><div><br></div><div><div style="color:rgb(8,8,8);font-family:"JetBrains Mono",monospace;font-size:9.8pt;white-space:pre"><div style="font-size:9.8pt"><span style="color:rgb(0,51,179)">from </span>PyQt6.QtWidgets <span style="color:rgb(0,51,179)">import </span>QApplication, QWidget, QTextEdit, QVBoxLayout<br><br><span style="color:rgb(0,51,179)">import </span>sys<br><br>app = QApplication(sys.argv)<br><br>window = QWidget()<br>layout = QVBoxLayout()<br>window.setLayout(layout)<br>text_edit = QTextEdit()<br>layout.addWidget(text_edit)<br><br></div><div style="font-size:9.8pt">text_edit.append(<span style="color:rgb(6,125,23)">"<h1>Main Header</h1><h2>Secondary Header</h2><p>Test paragraph.</p>"</span>)<br><br><span style="color:rgb(0,0,128)">print</span>(text_edit.toHtml())</div><div style="font-size:9.8pt">print()<br><span style="color:rgb(0,0,128)">print</span>(text_edit.toMarkdown())<br>window.show()<br><br>app.exec()<br></div></div></div><div><br></div><div>Two things to note of this code when executed. First, the appearance of the text displayed in the QTextEdit is correct and second, the printed html and markdown are correct, namely:</div><div><br></div><div><font face="monospace"><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN" "<a href="http://www.w3.org/TR/REC-html40/strict.dtd">http://www.w3.org/TR/REC-html40/strict.dtd</a>"><br><html><head><meta name="qrichtext" content="1" /><meta charset="utf-8" /><style type="text/css"><br>p, li { white-space: pre-wrap; }<br>hr { height: 1px; border-width: 0; }<br>li.unchecked::marker { content: "\2610"; }<br>li.checked::marker { content: "\2612"; }<br></style></head><body style=" font-family:'Segoe UI'; font-size:9pt; font-weight:400; font-style:normal;"><br><h1 style=" margin-top:18px; margin-bottom:12px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span style=" font-size:xx-large; font-weight:700;">Main Header</span></h1><br><h2 style=" margin-top:16px; margin-bottom:12px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span style=" font-size:x-large; font-weight:700;">Secondary Header</span></h2><br><p style=" margin-top:12px; margin-bottom:12px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;">Test paragraph.</p></body></html><br><br></font></div><div><font face="monospace"># Main Header<br><br>## Secondary Header<br><br>Test paragraph.<br></font></div><div><br></div><div>Now, let's make a minor modification to the above code. Instead of adding the entire desired HTML text to the QTextEdit all at once, let's append each line one after the other:</div><div><br></div><div><div style="color:rgb(8,8,8);font-family:"JetBrains Mono",monospace;font-size:9.8pt;white-space:pre"><span style="color:rgb(0,51,179)">from </span>PyQt6.QtWidgets <span style="color:rgb(0,51,179)">import </span>QApplication, QWidget, QTextEdit, QVBoxLayout<br><br><span style="color:rgb(0,51,179)">import </span>sys<br><br>app = QApplication(sys.argv)<br><br>window = QWidget()<br>layout = QVBoxLayout()<br>window.setLayout(layout)<br>text_edit = QTextEdit()<br>layout.addWidget(text_edit)<br><span style="color:rgb(140,140,140);font-style:italic"><br></span>text_edit.append(<span style="color:rgb(6,125,23)">"<h1>Main Header</h1>"</span>)<br>text_edit.append(<span style="color:rgb(6,125,23)">"<h2>Secondary Header</h2>"</span>)<br>text_edit.append(<span style="color:rgb(6,125,23)">"<p>Test paragraph.</p>"</span>)<br><br><span style="color:rgb(0,0,128)">print</span>(text_edit.toHtml())<br><span style="color:rgb(0,0,128)">print</span>(text_edit.toMarkdown())<br>window.show()<br><br>app.exec()<br></div></div><br clear="all"><div><div dir="ltr" class="gmail_signature" data-smartmail="gmail_signature"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div><span style="font-size:12.8px">The appearance of the text in the QTextEdit window is exactly the same as previously, but in the HTML and Markdown output, every line has been converted to a first level header:</span></div><div><span style="font-size:12.8px"><font face="monospace"><br></font></span></div><div><font face="monospace"><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN" "<a href="http://www.w3.org/TR/REC-html40/strict.dtd">http://www.w3.org/TR/REC-html40/strict.dtd</a>"><br><html><head><meta name="qrichtext" content="1" /><meta charset="utf-8" /><style type="text/css"><br>p, li { white-space: pre-wrap; }<br>hr { height: 1px; border-width: 0; }<br>li.unchecked::marker { content: "\2610"; }<br>li.checked::marker { content: "\2612"; }<br></style></head><body style=" font-family:'Segoe UI'; font-size:9pt; font-weight:400; font-style:normal;"><br><h1 style=" margin-top:18px; margin-bottom:12px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span style=" font-size:xx-large; font-weight:700;">Main Header</span></h1><br><h1 style=" margin-top:18px; margin-bottom:12px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span style=" font-size:x-large; font-weight:700;">Secondary Header</span></h1><br><h1 style=" margin-top:18px; margin-bottom:12px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;">Test paragraph.</h1></body></html><br><br># Main Header<br><br># Secondary Header<br><br># Test paragraph.</font><span style="font-size:12.8px"><br></span></div><div><span style="font-size:12.8px"><br></span></div><div>I'm pretty sure this behavior is wrong, particularly as the visual output in the GUI is identical in both cases (i.e., displaying the text correctly as a level 1 header, level 2 header, and general text paragraph). I suppose this could be a Qt6 bug and not PyQt6, but I don't use C so cannot directly test whether this behavior is unique to PyQt or general to Qt.</div><div><span style="font-size:12.8px"><br></span></div><div><span style="font-size:12.8px"><br></span></div><div><span style="font-size:12.8px">~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~</span><br></div>Michael S. Rosenberg, PhD</div><div dir="ltr">Director, Center for Biological Data Science<br><div>Virginia Commonwealth University<br></div><div><a href="mailto:msrosenberg@vcu.edu" target="_blank">msrosenberg@vcu.edu</a></div><div><a href="https://cbds.vcu.edu" target="_blank">https://cbds.vcu.edu</a><br></div><div><a href="https://www.rosenberglab.net" target="_blank">https://www.rosenberglab.net</a></div><div><div><span style="font-size:12.8px">~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~</span><br></div></div><div><div><i><font color="#0b5394" face="verdana, sans-serif">I often send emails outside standard work hours; </font></i></div><div><font color="#0b5394" face="verdana, sans-serif"><i>please do not feel obligated to respond outside of your</i><i>s.</i></font></div><span style="color:rgb(166,77,121);font-family:arial,sans-serif;font-style:italic"></span><div><i><font face="arial, sans-serif" color="#a64d79"><br></font></i></div></div></div></div></div></div></div></div>