Sunday, May 25, 2008

How to include rich text in a QImage

One method is to use a QTextDocument. In the example code fragment below we assume that text is a QString containing some rich text.

QTextDocument doc;
doc.setHtml(text);
double offsetV = doc.size().height()*0.5;
double offsetH = 0.;
painter->translate(QPointF(-offsetH, -offsetV));
doc.drawContents(painter);
painter->translate(QPointF(offsetH, offsetV));

This will give left-aligned text at the current position. For centre-aligned text use
   double offsetH = doc.size().width()*0.5;
or for right-aligned text
   double offsetH = doc.size().width();

No comments: