Ote
Ote

Reputation: 301

QGraphicsView to pdf

I have a big scene successfully displayed in my window with the help of QGraphicsView/QGraphicsScene with scroll bars for navigation. Everything works fine.

Now I would like now to render part of the scene in a pdf. The region to be render in the pdf should be the area visible in the window and the rectangles above and under the visible area.

I've tried that (to begin simply, I've ignored if horizontal scroll bar have been used) :

QPrinter myPrinter(QPrinter::ScreenResolution); 
myPrinter.setOrientation(QPrinter::Landscape);
myPrinter.setPaperSize(QPrinter::A4);
myPrinter.setOutputFormat(QPrinter::PdfFormat);
myPrinter.setPageMargins(0.0, 0.0, 0.0, 0.0, QPrinter::Point);

QPainter myPainter(&myPrinter);
m_pageWidth = myPrinter.width();
m_pageHeight = myPrinter.height();
myPainter.setViewport(0, 0, m_pageWidth, m_pageHeight);

QRectF viewRender = QRect(0.0, 0.0, m_pageWidth, m_pageHeight);

for(int i = 0; i < myScene->getNbPages(); i++)
{
    QRect viewScene = QRect(0, m_pageHeight * i, m_pageWidth, m_pageHeight);
    setSceneRect(viewScene);

    render(&myPainter, viewRender, viewScene);

    if(i + 1 < myScene->getNbPages())
        myPrinter.newPage();
}

But I don't get result as I would expect. For example in this function QGraphicsView::drawBackground(QPainter *painter, const QRectF &rect) rect's top left corner is not at 0, 0 for the first page, but a 107, 98, then at 107, 1585 (but page height is only 793 ?!) and so on...

Anyone understand what is going on ? Thanks for reading.

Upvotes: 0

Views: 2357

Answers (1)

phyatt
phyatt

Reputation: 19122

http://qt-project.org/doc/qt-4.8/qgraphicsview.html#mapToScene

http://qt-project.org/doc/qt-4.8/qgraphicsview.html#mapFromScene

Use one or the other of those appropriately and you should get the results you want.

For example you might try:

render(&myPainter, this->mapToScene(viewRender), viewScene);
// assuming this is your QGraphicsView instance

I demoed how to use this in another question I answered:

How to draw a point (on mouseclick) on a QGraphicsScene?

Hope that helps.

Upvotes: 1

Related Questions