Big QGraphicsScene 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.
-
The setSceneRect doesn't make sense to me in that context. Why do you do that?
I also don't see from your code which object's render function you call. It should be the Scene's.
-
setSceneRect() is here to make the view "watch" a particular area of the scene and by this way, render on each page, different areas of the scene. I was doing that previously when my scene did not had a large width and it worked well.
I call render on the view because I have to paint some things in the background and this is handle by the view. But when doing like that, items in my scene are also paint after the background is drawn.
In fact, I have two views, one for handling the rendering in my window, and one for rendering to the pdf. Both were doing what I want when my scene was not large enough to need horizontal scroll bar, but now that there is some, I'm having difficulties with the one in charge on rendering the scene to a pdf file. Due to that, I can't call render on the scene because backgrounds are different for the window or for the pdf.
-
One problem when rendering the content of a view is that changes you make might need the event loop to run.
As for painting differently for screen and pdf: I have another idea (though I admit I have never tried it myself): In the paint methods of those items that should paint differently, you can query the QPaintDevice associated with the painter, and behave differently.