Search code examples
qt4pdf-generationqgraphicssceneqpainterqprinter

Printing QGraphicsScene Produces Low Quality Output


My code roughly resembles this:

QGraphicsScene* pScene( pMap->scene() );

QPrinter printer;
printer.setResolution( 1200 ); //tried many values and QPrinter::PrinterMode
printer.setOutputFileName( "test.pdf" );
printer.setPageSize( QPrinter::A4 );
printer.setOutputFormat( QPrinter::PdfFormat );

QPainter painter( &printer );
pScene->render( &painter );
painter.end();

In my scene are some SVG images which display perfectly at the currently displayed resolution and at over 100 zoom scales further so quality of source is not an issue. However the PDF output is of terrible quality. I would happily render the output as an SVG or if it must be a static image at several times the current resolution/DPI to ensure it is clean and clear. If anyone has a suggestion on how to improve the output I would be very grateful. Thanks.

EDIT: It seems the output rendered into the PDF is directly tied to the shape/size of the view which is displaying the scene. E.g. if I shrink the view the quality gets worse. If I elongate it in one direction the output is elongated too. I am looking to output a consistent rendered drawing in high quality regardless of the view. I suspect I will need to put this scene into a new view of fixed dimensions using a larger scale/viewport to solve this issue.


Solution

  • If anyone needs an example of how to do this, check out qgscomposition.cpp::exportAsPDF within the QGIS GIT. The composition is an extension of a qGraphicsScene and should be directly applicable (with some rework) to anyone's task.
    http://qgis.org/api/2.8/qgscomposition_8cpp_source.html#l02651