PDA

View Full Version : How do I calculate the height of rendered HTML text in a QTextEdit ?



muenalan
24th January 2009, 09:26
I want to render linenumbers to the left of a QTextEdit; not just simple single font situation, but when there is a mix of fonts. So how do I calculate the height for each line of HTML text; where to place a QLabel with the line number ?

I thought of finding the height of the biggest char/font item used in a line of HTML text. Something like

qreal lineheight_line3 = lineHeightfromHTML( QString( "<h1>Hello</h1><p font="Verdena" size=8>world</p>" );

Or better something that returns an array of all fonts used per line, which then could be inspected with QFontMetric. But actually I already fail to find the api function that can give me individual lines of a QTextEdit, not just the plain text.

Thanks in advance,
Mu

jpn
24th January 2009, 17:49
Here's one way: http://websvn.kde.org/trunk/KDE/kdelibs/kjsembed/examples/editor/numberedtextview.cpp?view=markup&pathrev=521795. This has been discussed on the forums before as well. Doing a search might help.

muenalan
24th January 2009, 19:30
Thank you,

you SOLVED it.

PS: The solution is elegant as well, since it is not required to iterate through all styles/Fonts of a line, as I would have expected; the boundingRect gives enough information to place the line number marks.



Mu