PDA

View Full Version : 2 questions on QPrint + QImage + PDF



jiveaxe
11th January 2008, 16:13
Hi,
these are my 2 question?

1)
I have added "print to pdf" to my program; here the function:


void MainWindow::printToPdf()
{
QPrinter printer;
printer.setOrientation(QPrinter::Portrait);
printer.setOutputFileName("test.pdf");
printer.setOutputFormat(QPrinter::PdfFormat);

QImage image(700,1000,QImage::Format_ARGB32);

QPainter painter;
painter.setRenderHint(QPainter::Antialiasing, true);
painter.begin(&image);
// Draw some text to image
painter.end();

QPainter p;
p.begin(&printer);
p.drawImage(0, 0, image);
p.end();
}

When I click the first time on the print button the pdf is created in the right way, but if I click a second time the resulting pdf shows some ugly effects: or the contents of the two print actions overlaps or are present some black dots and lines (apart from the desired text).

I have also added delete &painter, delete &printer, delete &image at the end of the above code but I got segmentation fault calling the print function.

2)
If I have two qimage is it possible print a pdf with two pages, one for each qimage?

Thanks

jpn
11th January 2008, 18:03
Hi,
these are my 2 question?

Yes, they are your questions.


When I click the first time on the print button the pdf is created in the right way, but if I click a second time the resulting pdf shows some ugly effects: or the contents of the two print actions overlaps or are present some black dots and lines (apart from the desired text).
I suspect the problem is in "// Draw some text to image".



I have also added delete &painter, delete &printer, delete &image at the end of the above code but I got segmentation fault calling the print function.

Never ever do that. Stack objects get automatically destructed while they go out of scope.



If I have two qimage is it possible print a pdf with two pages, one for each qimage?

See QPrinter::newPage().

jiveaxe
11th January 2008, 18:28
I suspect the problem is in "// Draw some text to image".


Here is the smallest code which gives the problem:


painter.save();
painter.setPen(Qt::black);
painter.setFont(Strings::titleFont());
painter.drawText(QRect(5,5,200,43), Qt::AlignLeft, Strings::title);
painter.restore();



See QPrinter::newPage()


QPrinter printer;
printer.setOrientation(QPrinter::Portrait);
printer.setOutputFileName("test.pdf");
printer.setOutputFormat(QPrinter::PdfFormat);

QImage image1(730,1053,QImage::Format_ARGB32);
QImage image2(730,1053,QImage::Format_ARGB32);

QPainter painter1;
painter1.setRenderHint(QPainter::Antialiasing, true);
painter1.begin(&image1);
// Some drawings

QPainter painter2;
painter2.setRenderHint(QPainter::Antialiasing, true);
painter2.begin(&image2);
// Some drawings

QPainter p;
p.setRenderHint(QPainter::Antialiasing, true);
p.begin(&printer);
p.drawImage(0, 0, image1);
printer.newPage();
p.drawImage(0, 0, image2);
p.end();

Am I right?

jpn
11th January 2008, 18:39
Oh, I think I realized the problem. I guess you should fill the image for example with Qt::white.

jiveaxe
11th January 2008, 18:46
Oh, I think I realized the problem. I guess you should fill the image for example with Qt::white.

You are right. Now it looks perfect.

Thanks

jpn
16th January 2008, 13:06
By the way, why are you using so many painter objects? Notice that you can save and restore the state state of a QPainter object if needed.