{
text.
setPageSize(QSize(rect.
width(), QWIDGETSIZE_MAX
));
const int height = qRound(layout->documentSize().height());
int y = rect.y();
if (flags & Qt::AlignBottom)
y += (rect.height() - height);
else if (flags & Qt::AlignVCenter)
y += (rect.height() - height)/2;
context.
palette.
setColor(QPalette::Text, painter
->pen
().
color());
painter->save();
painter->translate(rect.x(), y);
layout->draw(painter, context);
painter->restore();
}
void drawRichText(QPainter *painter, const QRect &rect, int flags, QTextDocument &text)
{
text.setPageSize(QSize(rect.width(), QWIDGETSIZE_MAX));
QAbstractTextDocumentLayout* layout = text.documentLayout();
const int height = qRound(layout->documentSize().height());
int y = rect.y();
if (flags & Qt::AlignBottom)
y += (rect.height() - height);
else if (flags & Qt::AlignVCenter)
y += (rect.height() - height)/2;
QAbstractTextDocumentLayout::PaintContext context;
context.palette.setColor(QPalette::Text, painter->pen().color());
painter->save();
painter->translate(rect.x(), y);
layout->draw(painter, context);
painter->restore();
}
To copy to clipboard, switch view to plain text mode
If you need additional operations like heightForWidth() or textSize() you can have a look at the implementation of QwtRichTextEngine in the Qwt package.
And yes, you can ask yourself, why you have to write this strange code for such a simple task.
HTH,
Uwe
Bookmarks