[PyQt] Incorrect HTML (Rich Text) parsing/rendering.

mir amicitas amicitas at gmail.com
Mon Mar 23 06:57:03 GMT 2009


I am having problems with rendering HTML (Rich Text) in PyQt.

I am using PyQt-Py2.6-gpl-4.4.4-2 with Python 2.6.1.

These errors show up with any widget that can display Rich Text.
For my HTML display example I am using a QTextBrowser.

The problems involve incorrect closing of various tags.  Even more
strange is that the behavior is different if I use
QTextBrowser.setHtml() or QTextBrowser.setSource().

For the example that I am attaching below there are two errors.
1.  The <div> tags around the title are not being closed until after
the next line of text.
2.  The list is not closed until the end of the file  (<ol> and <li> tags).

Problem #1 happens with both setHtml() and setSource().
Problem #2 happens only with setSource.
Clicking on a link has equivalent behavior to using setSource()

Both the display of the HTML in the widget, and the output from
QTextBrowser.toHtml() are incorrect.

I do not know if this is a Qt or a PyQt issue, but I thought I would
ask for help here first.

Included at the bottom of this message is a representative program and
the output (simplified).
Attached is the program and output (raw).

Thanks in advance for any help.

Amicitas


=======================
html_display.py
=======================
from PyQt4 import QtCore, QtGui
import sys

# Set up the base window.
class MainWindow(QtGui.QMainWindow):
    def __init__(self, parent):
        QtGui.QMainWindow.__init__(self, parent)

        html = r"""
<html>
<body>
<div align=center>This should be centered.</div>
<br> This should be left justified.
<ol>
  <li>Item 1</li>
  <li>Item 2</li>
</ol>
<br>
<br>This should not be part of a list.
<br>
<br><a href="simple.html">simple.html</a>
</body>
</html>
"""

        self.browser = QtGui.QTextBrowser(self)
        self.setCentralWidget(self.browser)
        self.resize(600,400)

        self.browser.setHtml(html)
        filename = 'output_setHtml.html'
        with open(filename, 'w') as file:
            file.write(self.browser.toHtml())

        source = QtCore.QUrl('simple.html')
        self.browser.setSource(source)
        filename = 'output_setSource.html'
        with open(filename, 'w') as file:
            file.write(self.browser.toHtml())

# Initialize the Qt application
app = QtGui.QApplication(sys.argv)

# Start the program.
if __name__ == '__main__':
    main_window = MainWindow(None)
    main_window.show()
    sys.exit(app.exec_())


=======================
simple.html - Input HTML
=======================
<html>
<body>
<div align=center>This should be centered.</div>
<br> This should be left justified.
<ol>
  <li>Item 1</li>
  <li>Item 2</li>
</ol>
<br>
<br>This should not be part of a list.
<br>
<br><a href="simple.html">simple.html</a>
</body>
</html>



=======================
output_setHtml.html - Output HTML
(For clarity I have removed all css and added white space)
=======================
<!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>
<p>
  This should be centered.
  <br />This should be left justified.
</p>
<ol>
  <li>Item 1 </li>
  <li>Item 2</li>
</ol>
<p>
  <br />
  <br />This should not be part of a list.
  <br />
  <br /><a href="simple.html"><span>simple.html</span></a>
</p>
</body>

</html>



=======================
output_setSource.html - Output HTML
(For clarity I have removed all css and added white space)
=======================
<!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>
<p align="center">
  This should be centered.
  <br />This should be left justified.
</p>
<ol>
  <li>Item 1 </li>
  <li>Item 2
    <br />
    <br />This should not be part of a list.
    <br />
    <br /><a href="simple.html"><span>simple.html</span></a>
  </li>
</ol>
</body>

</html>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.riverbankcomputing.com/pipermail/pyqt/attachments/20090323/f45f7de0/output_setHtml-0001.html
-------------- next part --------------
A non-text attachment was scrubbed...
Name: html_display.py
Type: application/octet-stream
Size: 1224 bytes
Desc: not available
Url : http://www.riverbankcomputing.com/pipermail/pyqt/attachments/20090323/f45f7de0/html_display-0001.obj
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.riverbankcomputing.com/pipermail/pyqt/attachments/20090323/f45f7de0/output_setSource-0001.html
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.riverbankcomputing.com/pipermail/pyqt/attachments/20090323/f45f7de0/simple-0001.html


More information about the PyQt mailing list