Results 1 to 6 of 6

Thread: 2 questions on QPrint + QImage + PDF

  1. #1
    Join Date
    Aug 2007
    Posts
    244
    Thanks
    42
    Thanked 8 Times in 8 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default 2 questions on QPrint + QImage + PDF

    Hi,
    these are my 2 question?

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

    Qt Code:
    1. void MainWindow::printToPdf()
    2. {
    3. QPrinter printer;
    4. printer.setOrientation(QPrinter::Portrait);
    5. printer.setOutputFileName("test.pdf");
    6. printer.setOutputFormat(QPrinter::PdfFormat);
    7.  
    8. QImage image(700,1000,QImage::Format_ARGB32);
    9.  
    10. QPainter painter;
    11. painter.setRenderHint(QPainter::Antialiasing, true);
    12. painter.begin(&image);
    13. // Draw some text to image
    14. painter.end();
    15.  
    16. p.begin(&printer);
    17. p.drawImage(0, 0, image);
    18. p.end();
    19. }
    To copy to clipboard, switch view to plain text mode 

    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
    Giuseppe CalÃ

  2. #2
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: 2 questions on QPrint + QImage + PDF

    Quote Originally Posted by jiveaxe View Post
    Hi,
    these are my 2 question?
    Yes, they are your questions.

    Quote Originally Posted by jiveaxe View Post
    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".

    Quote Originally Posted by jiveaxe View Post
    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.

    Quote Originally Posted by jiveaxe View Post
    If I have two qimage is it possible print a pdf with two pages, one for each qimage?
    See QPrinter::newPage().
    J-P Nurmi

  3. #3
    Join Date
    Aug 2007
    Posts
    244
    Thanks
    42
    Thanked 8 Times in 8 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: 2 questions on QPrint + QImage + PDF

    Quote Originally Posted by jpn View Post
    I suspect the problem is in "// Draw some text to image".
    Here is the smallest code which gives the problem:

    Qt Code:
    1. painter.save();
    2. painter.setPen(Qt::black);
    3. painter.setFont(Strings::titleFont());
    4. painter.drawText(QRect(5,5,200,43), Qt::AlignLeft, Strings::title);
    5. painter.restore();
    To copy to clipboard, switch view to plain text mode 

    Quote Originally Posted by jpn View Post
    Qt Code:
    1. QPrinter printer;
    2. printer.setOrientation(QPrinter::Portrait);
    3. printer.setOutputFileName("test.pdf");
    4. printer.setOutputFormat(QPrinter::PdfFormat);
    5.  
    6. QImage image1(730,1053,QImage::Format_ARGB32);
    7. QImage image2(730,1053,QImage::Format_ARGB32);
    8.  
    9. QPainter painter1;
    10. painter1.setRenderHint(QPainter::Antialiasing, true);
    11. painter1.begin(&image1);
    12. // Some drawings
    13.  
    14. QPainter painter2;
    15. painter2.setRenderHint(QPainter::Antialiasing, true);
    16. painter2.begin(&image2);
    17. // Some drawings
    18.  
    19. p.setRenderHint(QPainter::Antialiasing, true);
    20. p.begin(&printer);
    21. p.drawImage(0, 0, image1);
    22. printer.newPage();
    23. p.drawImage(0, 0, image2);
    24. p.end();
    To copy to clipboard, switch view to plain text mode 

    Am I right?
    Last edited by jiveaxe; 11th January 2008 at 18:41.
    Giuseppe CalÃ

  4. #4
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: 2 questions on QPrint + QImage + PDF

    Oh, I think I realized the problem. I guess you should fill the image for example with Qt::white.
    J-P Nurmi

  5. The following user says thank you to jpn for this useful post:

    jiveaxe (11th January 2008)

  6. #5
    Join Date
    Aug 2007
    Posts
    244
    Thanks
    42
    Thanked 8 Times in 8 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: 2 questions on QPrint + QImage + PDF

    Quote Originally Posted by jpn View Post
    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
    Giuseppe CalÃ

  7. #6
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: 2 questions on QPrint + QImage + PDF

    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.
    J-P Nurmi

Similar Threads

  1. Replies: 12
    Last Post: 7th September 2011, 17:37
  2. QShared
    By sabeesh in forum Qt Programming
    Replies: 11
    Last Post: 11th October 2007, 13:40

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.