[Qt-interest] QGraphicsView render to PDF not working

Deepak Chandran dchandran1 at yahoo.com
Thu Jan 20 03:52:05 CET 2011


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));
    }



      
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20110119/84054866/attachment.html 


More information about the Qt-interest-old mailing list