PDA

View Full Version : How obtain QTextDocument height ?



jiveaxe
25th October 2007, 18:27
Hi,
I'm trying to paint different blocks of html text in a column; the text to be written is read from a database so it changes at runtime; the text must fit the column's width so it must wordwrap.

For obtaing this i have used QTextDocument and its ability to draw html text; since QTextDocument doesn't have a function which returns the final height I've used a code (by marcel) found in an other thread:


QFontMetrics fm(Strings::proconsFont());
float rw = float(columnWidth);
float tw = fm.width(text);
float ratio = tw/rw;
int lines = std::ceil(ratio);
QAbstractTextDocumentLayout::PaintContext context;
QTextDocument doc;
doc.setDefaultFont(Strings::proconsFont());
doc.setTextWidth(columnWidth);
doc.setHtml(text);
doc.documentLayout()->draw(painter, context);
painter->translate(0, lines*fm.height());

This works quite good but sometime the last row of a block and the first of that just below overlap.

QTextDocument organizes the painted text not as is supposed with the lines trick:


|-------cloumnWidth------|
this is a very long senten
ce wrapped in two rows.

but:


|-------cloumnWidth------|
this is a very long
sentence wrapped in two
rows.

So, instead of two rows the text is painted in three and cause overlaps with the subsequent block that is painted at 2*fm.height() instead of 3*fm.height() (vertically).

Is there a different manner for obtaining QTextDocument height? Or is there a totally different approach not involving QTextDocument?

Thanks, and sorry for my english.

Giuseppe

jacek
25th October 2007, 22:18
Try QAbstractTextDocumentLayout::documentSize().

jiveaxe
26th October 2007, 08:27
Thank you jacek,
you tips work perfectly; doc.documentLayout()->documentSize().height() returns the desired value.

Regards