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:

Qt Code:
  1. QFontMetrics fm(Strings::proconsFont());
  2. float rw = float(columnWidth);
  3. float tw = fm.width(text);
  4. float ratio = tw/rw;
  5. int lines = std::ceil(ratio);
  6. QAbstractTextDocumentLayout::PaintContext context;
  7. doc.setDefaultFont(Strings::proconsFont());
  8. doc.setTextWidth(columnWidth);
  9. doc.setHtml(text);
  10. doc.documentLayout()->draw(painter, context);
  11. painter->translate(0, lines*fm.height());
To copy to clipboard, switch view to plain text mode 

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:

Qt Code:
  1. |-------cloumnWidth------|
  2. this is a very long senten
  3. ce wrapped in two rows.
To copy to clipboard, switch view to plain text mode 

but:

Qt Code:
  1. |-------cloumnWidth------|
  2. this is a very long
  3. sentence wrapped in two
  4. rows.
To copy to clipboard, switch view to plain text mode 

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