Results 1 to 9 of 9

Thread: Render QGraphicsScene to a QPrinter to export PDF

  1. #1
    Join Date
    Mar 2012
    Posts
    12
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Render QGraphicsScene to a QPrinter to export PDF

    Hi there,

    Ok. Now I am going crazy...

    Can anyone tell me why this works:

    Qt Code:
    1. void Partitura::print()
    2. {
    3. QPrinter printer(QPrinter::HighResolution);
    4. printer.setPageSize(QPrinter::A4);
    5. printer.setOrientation(QPrinter::Portrait);
    6. QPrintDialog dlg(&printer);
    7. if(dlg.exec()==QDialog::Accepted) {
    8. QPainter p(&printer);
    9. scene->render(&p);
    10. p.end();
    11. }
    12. }
    To copy to clipboard, switch view to plain text mode 

    ... and this does not work:

    Qt Code:
    1. void Partitura::exportPDF()
    2. {
    3. QString fileName = QFileDialog::getSaveFileName(this, "Export PDF",
    4. QString(), "*.pdf");
    5. QPrinter printer(QPrinter::HighResolution);
    6. printer.setPageSize(QPrinter::A4);
    7. printer.setOrientation(QPrinter::Portrait);
    8. printer.setOutputFormat(QPrinter::PdfFormat);
    9. printer.setOutputFileName(fileName);
    10.  
    11. QPainter p(&printer);
    12. scene->render(&p);
    13. p.end();
    14. }
    To copy to clipboard, switch view to plain text mode 
    scene is a QGraphicsScene object declared as global.
    The debugger returns a stack overflow when running the 2nd function.
    If i comment the line scene->render(&p); the problem disappears, but obviously i don't get the scene contents on the pdf...

    Thanks in advance.
    Tiago Campos
    Last edited by wysota; 19th March 2012 at 11:26. Reason: missing [code] tags

  2. #2
    Join Date
    Mar 2012
    Posts
    12
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Render QGraphicsScene to a QPrinter to export PDF

    Please someone, I need help on this issue.

  3. #3
    Join Date
    Sep 2011
    Location
    Manchester
    Posts
    538
    Thanks
    3
    Thanked 106 Times in 103 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Render QGraphicsScene to a QPrinter to export PDF

    1st - use [CODE] tags, it's much easier to read the code.

    2nd: this works perfectly fine
    Qt Code:
    1. void MainWindow::print()
    2. {
    3. QPrinter printer( QPrinter::HighResolution );
    4. printer.setPageSize( QPrinter::A4 );
    5. printer.setOrientation( QPrinter::Portrait );
    6. printer.setOutputFormat( QPrinter::PdfFormat );
    7. printer.setOutputFileName( "test.pdf" ); // file will be created in your build directory (where debug/release directories are)
    8.  
    9.  
    10. if( !p.begin( &printer ) )
    11. {
    12. qDebug() << "Error!";
    13. return;
    14. }
    15. this->scene->render( &p );
    16. p.end();
    17. }
    To copy to clipboard, switch view to plain text mode 
    One thing i've noticed, for some reason I was not able to save file for example on c:\ (w7).

    Create small compilable example of your problem and post it here.

  4. #4
    Join Date
    Mar 2012
    Posts
    12
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Render QGraphicsScene to a QPrinter to export PDF

    Thank you very much for your answer.

    I tried exactly the same code you supplied without success.
    The QGraphicsScene I'm trying to export to PDF has a large number of objects, including lines, ellipses and QGraphicsSvgItem objects using sharing renderer from QSvgRenderer objects.

    Sorry but it is hard to post a small compilable example since the code is now very complex.

    I noticed that if I clear the scene with scene->clear(), rendering the scene to the painter works and I'm able to save an empty PDF. So it probably has to do with the the type of objects I am using inside the scene...

  5. #5
    Join Date
    Sep 2011
    Location
    Manchester
    Posts
    538
    Thanks
    3
    Thanked 106 Times in 103 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Render QGraphicsScene to a QPrinter to export PDF

    If that's the case, then save pdf after adding each item and see which one breaks it.
    It may be that single item stops the renderer or may be that you can't render whole family (ie svq items).

  6. #6
    Join Date
    Mar 2012
    Posts
    12
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Render QGraphicsScene to a QPrinter to export PDF

    That's an excellent idea. Thank you for the tip.


    Added after 1 22 minutes:


    Quote Originally Posted by Spitfire View Post
    If that's the case, then save pdf after adding each item and see which one breaks it.
    It may be that single item stops the renderer or may be that you can't render whole family (ie svq items).
    Hello again.

    It seems that the first item added to the scene makes it impossible to export to pdf (a line added with scene->addLine(line, pen)). Before adding it, a pdf is created sucessfully.
    Any idea?

    The QGraphicsSvgItem objects in the scene are exported to pdf... I don't understand the problem with the lines...
    Last edited by tcampos; 23rd March 2012 at 15:40.

  7. #7
    Join Date
    Sep 2011
    Location
    Manchester
    Posts
    538
    Thanks
    3
    Thanked 106 Times in 103 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Render QGraphicsScene to a QPrinter to export PDF

    What are the line points?
    What's the pen?

    If you coment out this line does every other line causes the same issue?

    If yes, can you create very minimal compilable example of the issue for others to test?
    I can't see you mentioning what qt version you're using.

  8. #8
    Join Date
    Mar 2012
    Posts
    12
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Render QGraphicsScene to a QPrinter to export PDF

    I'll do that.

    Used pen and example of one of the lines:

    QPen pen(Qt::black, 1);
    QLineF line(QPoint(0, 140), QPoint(880, 140));

    Qt version 4.7.4.

    I'm almost sure that every single I try to add to the scene causes this issue.
    Last edited by tcampos; 23rd March 2012 at 16:54.

  9. #9
    Join Date
    Mar 2012
    Posts
    12
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Render QGraphicsScene to a QPrinter to export PDF

    After your suggestions I tried to isolate the problem with the exact same code, restricted to the needed part. To my surprise the program worked correctly. The only difference was that the Ui of the window that contained that graphicsview object was the mainwindow of the program.

    In the application I'm developing the window which I'm working in and having that issues is another QMainWindow object that receives the mainwindow by pointer reference. Maybe there is a bug related to it. At least it is what I am supposing...

    Anyway I'm not using SVG anymore. I replaced the objects I needed by QPainterPath and QGraphicsPathItem objects relying on fonts (musical symbols from gonville font). Everything works correctly now!

    Again, thank you for your help.
    Last edited by tcampos; 29th March 2012 at 17:10.

Similar Threads

  1. QPrinter & QPainter render() problem
    By Mannion in forum Qt Programming
    Replies: 31
    Last Post: 28th March 2012, 17:18
  2. Replies: 6
    Last Post: 14th September 2010, 22:18
  3. How to render the contents of QPrinter to QWidget
    By nifei in forum Qt Programming
    Replies: 0
    Last Post: 6th March 2009, 04:25
  4. QGraphicsScene render thread
    By nicolas1 in forum Qt Programming
    Replies: 7
    Last Post: 26th October 2008, 12:57
  5. QGraphicsScene render() crashes
    By iebele in forum Qt Programming
    Replies: 0
    Last Post: 29th April 2008, 13:38

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.