[Interest] Black background with QPrinter

Joshua Grauman jnfo-c at grauman.com
Fri Mar 17 02:03:51 CET 2017


Hi all,

I am trying to print a QTextDocument to PDF using QPrinter like the 
following. It works great. But now I'm trying to print to a PDF with a 
black background and I can't figure out how. If I do painter.fillRect() it 
doesn't cover the margins (margins are white). I've tried changing the 
document() stylesheet with no success. Does anyone know what is the best 
way to have a different color background for a PDF? Thanks!


void TextEdit::print(QPrinter *printer)
{
   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();

   for(int page=0; page<pageCount; page++)
   {
     painter.save();
     painter.scale(scale, scale);

     QRectF view(0, page*pageHeight, pageWidth, pageHeight);

     painter.translate(0, -page*pageHeight);

     document()->drawContents(&painter, view);
       painter.restore();

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



More information about the Interest mailing list