[Qt-interest] Printing QTextDocument

Josh jnfo-c at grauman.com
Thu Jun 25 08:02:57 CEST 2009


Hello,

I'm wondering if anyone can help me. The following code is designed to print a 
QTextDocument. It prints the first two pages perfectly, but then the third and 
fourth page the coordinates system is way off (vertically). For the third page, 
I can barely see the bottom of what should be on the page at the very top of my 
printed page. The fourth page is blank and it shouldn't be. Any thoughts? I'm 
banging my head on this one. Thanks. I'm thinking something must be wrong in 
the for loop... - Josh

   QPrinter printer(QPrinter::ScreenResolution);
   printer.setPaperSize(QPrinter::Letter);
   printer.setPageMargins(0.5,0.5,0.5,0.5,QPrinter::Inch);

   QPrintDialog *dialog = new QPrintDialog(&printer, this);
   dialog->setWindowTitle("Print Document");
   if(dialog->exec() != QDialog::Accepted)
      return;

   QPainter painter;
   painter.begin(&printer);

   float pageWidth=document()->size().width();
   float scale = printer.pageRect().width()/pageWidth;
   float pageHeight=printer.pageRect().height()/scale;

   document()->setPageSize(QSizeF(pageWidth,pageHeight));

   int pageCount=document()->pageCount();

   painter.scale(scale,scale);
   QAbstractTextDocumentLayout::PaintContext paintContext;
   paintContext.palette.setColor(QPalette::Text, Qt::black);

   for(int page=0; page<pageCount; page++)
   {
     paintContext.clip = QRectF (0, page*pageHeight, pageWidth, pageHeight);
     painter.translate(0, -page*pageHeight);
     document()->documentLayout()->draw(&painter, paintContext);

     if(page != pageCount-1)
       printer.newPage();
   }
   painter.end();



I was wondering if it could have to do with limitations is QPainter, but I 
don't think my coordinates are that large...?

cf. http://doc.trolltech.com/4.5/qpainter.html#limitations

Limitations

If you are using coordinates with Qt's raster-based paint engine, it is 
important to note that, while coordinates greater than +/- 215 can be used, any 
painting performed with coordinates outside this range is not guaranteed to be 
shown; the drawing may be clipped. This is due to the use of short int in the 
implementation.





More information about the Qt-interest-old mailing list