0

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.

Ote
  • 301
  • 1
  • 5
  • 16

1 Answers1

1

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.

Community
  • 1
  • 1
phyatt
  • 18,472
  • 5
  • 61
  • 80
  • Thanks for your answer. Indeed, I guess there's something wrong with my coordinates, I was going in that direction. Although what you suggest does not change anything for now... – Ote Apr 19 '13 at 00:51
  • What about using mapFromScene on the same variable? Or on your viewScene variable? – phyatt Apr 19 '13 at 01:41
  • Definitly not `mapFromScene` because with that, in `drawBackground(QPainter *painter, const QRectF &rect)` rect's top left corner is at 107, 0.00007 with width 66000000 and height 7600000... I'm going crazy with that – Ote Apr 19 '13 at 20:28