PDA

View Full Version : QTextDocument: print or drawContents?



giusepped
11th November 2009, 01:53
I would like to understand which is the difference between print() and drawContenst() methods of QTextDocument.
I know that print() can handle page breaking while drawContents not.
So far, I have been using drawContents() because I can handle easily complex layout.
However, if I have to put in the TextDocument a complex html I use
document->setHtml(string).
But: when I use drawContents(&painter,printer->paperRect())
I get always a very small and unreadable square of printed text on the output pages.

If I use a transformation like:


document->adjustSize();
QTransform tra;
tra.scale(11,11);
painter.setTransform(tra);


the page is visible. Why shall I use the transofmration?
Is there a method to set a transformation independent of the printer device?
Thank you

wysota
11th November 2009, 09:34
You have to remember that your printer has a different resolution than your screen. If your screen has 96dpi and your printer has 1200dpi, the difference is roughly 11 times which would explain the figures. You need to use QPrinter and QPrintDialog methods to initialize the printer paint device properly.

giusepped
12th November 2009, 01:59
I use one class for printing on pdf and QPrintPreview to print on other devices.
Printing on pdf is ok, no problem, all pages are correctly handled, both on windows and linux.
But when I run the app on windows, the preview printing does not work perfectly. I mean, margins are sometimes different.
However, I use QPainter painter(printer), i.e. the painter is initialized with the printer.
And, if I debug. I obtain:
painter.viewport().width() = printer.pageRect().width()

p.s. what I am talking about is this http://bugreports.qt.nokia.com/browse/QTBUG-3290

G

akobor
9th November 2010, 08:20
Hello everyone,

Althought this is an old thread, i post my solution took me a few hours...
By default the document is painting with qt::screenresolution and event scaling this to the right size the painting quality on paper is ugly(draft).
But using the code :

QPrinter printer(QPrinter::HighResolution);
QPainter painter(&printer);
QTexDocument doc;
doc.documentLayout()->setPaintDevice(painter.device());
doc.setHtml("htmlcontent");
doc.drawContents(painter);