[Qt-interest] QGraphicsView render to PDF not working

sarvesh saran aquarian.thunder at gmail.com
Thu Jan 20 04:58:55 CET 2011


Hi Deepak,


Can you try this instead..you may modify the options set in pdfPrinter
if you want to..


    fileName = "test.pdf";
    QPrinter *pdfPrinter = new QPrinter;
    QPainter *pdfPainter = new QPainter;
    pdfPrinter->setOutputFormat(QPrinter::PdfFormat);
    pdfPrinter->setPaperSize(QSize(SCENE_SIZE, SCENE_SIZE), QPrinter::Point);
    pdfPrinter->setFullPage(true);
    pdfPrinter->setOutputFileName(fileName);
    pdfPainter->begin(pdfPrinter);
    scene.render(pdfPainter);
    pdfPainter->end();
    delete pdfPainter;
    delete pdfPrinter;


thanks,

Sarvesh Saran


On Thu, Jan 20, 2011 at 8:22 AM, Deepak Chandran <dchandran1 at yahoo.com>wrote:

> I have a program using QGraphicsScene. The scene has lots of object (with
> shades, curves, etc). I want to save the scene as a PDF, but it is not
> working. Exporting as a QImage and writing that image to a file works fine,
> so I am not sure exactly where the problem is. Below is a summary of the
> code. The code works if I use a QImage and render the scene onto it, but it
> does not work if I use a PDF file.
>
> //main print code
>         QPrinter printer(QPrinter::HighResolution);
>         printer.setOutputFormat(QPrinter::PdfFormat);
>         printer.setOrientation(QPrinter::Landscape);
>         printer.setPageSize(QPrinter::B0);  //I also tried A4
>         printer.setOutputFileName(fileName);
>
>         //scene is a MyGraphicsScene pointer (see below)
>         scene->print(&printer);
>
> //inside MyGraphicsScene class
>     void MyGraphicScene::print(QPaintDevice * printer, const QRectF&
> region) const
>     {
>         QList<QGraphicsView*> list = views();
>         if (list.isEmpty() || !list[0]) return;
>         QGraphicsView * view = list[0];
>
>         QPainter painter(printer);
>         painter.setBackground(QBrush(Qt::white));
>         painter.setRenderHints(QPainter::Antialiasing);
>
>         QRectF rect( 0, 0, printer->width(), printer->height());
>         painter.fillRect(rect,QBrush(Qt::white));
>
>         QPointF p1 = view->mapFromScene(region.topLeft()),
>                         p2 = view->mapFromScene(region.bottomRight());
>
>         view->render(&painter,QRectF(p1,p2));
>     }
>
>
> _______________________________________________
> Qt-interest mailing list
> Qt-interest at qt.nokia.com
> http://lists.qt.nokia.com/mailman/listinfo/qt-interest
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20110120/5614eedd/attachment.html 


More information about the Qt-interest-old mailing list