PDA

View Full Version : RichText printing with HighResolution printer



ghorwin
20th July 2009, 15:18
Hi there,

I have trouble printing a rich text document to a printer with HighResolution settings.

So far I use something in the lines of:


QAbstractTextDocumentLayout* layout = text.documentLayout();

QAbstractTextDocumentLayout::PaintContext context;
context.palette.setColor(QPalette::Text, painter->pen().color());
layout->draw(painter, context);

whereas text is a QTextDocument, and painter is a QPainter created for a printer object. The printer was initialized with QPrinter::HighResolution.

It seems that printing with high-resolution is somewhat tricky. For example, the font metrics returned have to be scaled manually to obtain a correct line spacing. And maybe that's also the reason why the draw() function of QAbstractTextDocumentLayout doesn't work as expected?

Any suggestions are welcome!
Andreas

ghorwin
31st July 2009, 10:04
Heureka!

The Qt-Documentation is somewhat sparse at this point. In any case when printing with HighResolution you always have to specify the QPaintDevice before drawing. This applies to QFontMetrics where you need to instantiate the instance with


QFontMetrics fm(myFont, p->device()); // p = QPainter *

Also, for the rich-text printing, you need to set the QPaintDevice before drawing. The code snipped below shows how it works:



QAbstractTextDocumentLayout* layout = text.documentLayout();

layout->setPaintDevice(painter->device()); // this is needed for HighRes printing

QAbstractTextDocumentLayout::PaintContext context;
context.palette.setColor(QPalette::Text, painter->pen().color());
layout->draw(painter, context);