[Qt-interest] Finding the displayed width of rich text

John Posner jjposner at optimum.net
Sun Dec 20 23:39:51 CET 2009


Tony Rietwyk wrote:
> I suggest using QTextDocument with the same text.  Then use size or
> idealSize. 
>   
idealSize() does the job -- thanks! Here's my proof-of-concept Python 
program:

#------------------------
import PyQt4
from PyQt4.QtGui import *

html_fraction = """
<html>
<head><style>
p {font-family: Times; font-size: 48pt; font-weight: bold;}
</style></head>
<body><p><sup>3</sup>&frasl;<sub>7</sub></p></body>
</html>
"""

app = QApplication([])
ted = QTextEdit()
doc = ted.document()
doc.setHtml(html_fraction)
ted.setDocument(doc)
print doc.idealWidth()  # output: 38.0

ted.show()
app.exec_()
#------------------------

-John




More information about the Qt-interest-old mailing list