PDA

View Full Version : Draw on QPrinter: color gets lost



FelixB
2nd February 2011, 12:48
Hi,

I want to print something. That's not a big problem: I create a QPrinter and a QPainter to paint on the QPrinter. I can draw an image and some text. I also want to draw some lines. In the preview window (QPrintPreviewDialog), everything looks fine. But, when I print to PDF with an external PDF-Printer or to a real printer, the lines lose their color and appear in black. The Textcolor remains at it should be...


void MyClass::Print(bool preview)
{
QPrinter *printer = new QPrinter(QPrinter::HighResolution;
printer->setPageMargins(20,20,20,20,QPrinter::DevicePixel);
printer->setColorMode(QPrinter::Color);
printer->setPageSize(QPrinter::A4);
printer->setPrintRange(QPrinter::AllPages);

if(preview)
{
QPrintPreviewDialog ppd(printer);
connect(&ppd, SIGNAL(paintRequested(QPrinter*)), this, SLOT(Print(QPrinter*)));
ppd.exec();
}
else
{
QPrintDialog *printDialog = new QPrintDialog(printer, CMainFrame::GetMainFrame().GetMainWin());
if (printDialog->exec())
Print(printer);
}
delete printer;
}


void MyClass::Print(QPrinter* printer)
{
//this is just a demonstration. My "real" code makes more sense ;)

QPainter p(printer);
QRectF border(QPoint(0,0), printer->pageRect().size() - QSize(1,1));

p.setPen(Qt::red);
p.drawRect(border);

QDateTime time = QDateTime::currentDateTime();
QString timestr = time.toString("dddd, MMMM d, yyyy hh:mm:ss");
p.drawText(400, 400, timestr);

p.drawLine(QLine(1000, 2000, 1800, 3500));
}

see the attached results. Preview: 5856, Print:5857

what can I do to print colored lines? I could draw everything onto a QPixmap first and
print this QPixmap. But I think this may be too much memory-consuming, especially when printing in HighResolution mode...

thanks!
Felix

FelixB
3rd February 2011, 09:26
I solved it! I have to create a pen and set the width explicitly. weird...


QPen pen(Qt::red);
pen.setWidth(1);
p.setPen(pen);