Results 1 to 4 of 4

Thread: GraphicsScene print

  1. #1
    Join Date
    Jan 2006
    Location
    Sta. Eugènia de Berga (Vic - Barcelona - Spain)
    Posts
    869
    Thanks
    70
    Thanked 59 Times in 57 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default GraphicsScene print

    Hi,

    I'm trying to print a QGraphicsScene and I'm having some problems with the resulting size.

    Qt Code:
    1. int iDPIs = 300;
    2. qreal qResolutionMmPx = 1 / ((double)iDPIs / 25.4); //mm / px
    3.  
    4. QPrinter qPrinter;
    5. QString qPDFFileName = "painter.pdf";
    6. qPrinter.setOutputFormat(QPrinter::PdfFormat);
    7. qPrinter.setOutputFileName(qPDFFileName);
    8. qPrinter.setPageSize(QPrinter::A4);
    9. qPrinter.setResolution(iDPIs);
    10.  
    11. QPainter qPainter(&qPrinter);
    12.  
    13. qPainter.drawRect(50/qResolutionMmPx,50/qResolutionMmPx,100/qResolutionMmPx,100/qResolutionMmPx);
    To copy to clipboard, switch view to plain text mode 
    This draws a 100x100mm rect perfectly.

    This code tryies to print using a graphics scene:
    Qt Code:
    1. int iDPIs = 300;
    2. qreal qResolutionMmPx = 1 / ((double)iDPIs / 25.4); //mm / px
    3.  
    4. pqScene = new QGraphicsScene(this);
    5. ui.graphicsView->setScene(pqScene);
    6. pqScene->addRect(0,0,100/qResolutionMmPx,100/qResolutionMmPx,QPen(Qt::blue)); //100x100mm desired rect
    7.  
    8. QPrinter qPrinter;
    9. QString qPDFFileName = "scene.pdf";
    10. qPrinter.setOutputFormat(QPrinter::PdfFormat);
    11. qPrinter.setOutputFileName(qPDFFileName);
    12. qPrinter.setPageSize(QPrinter::A4);
    13. qPrinter.setResolution(iDPIs);
    14.  
    15. QPainter qPainter(&qPrinter);
    16. pqScene->render(&qPainter);
    To copy to clipboard, switch view to plain text mode 
    This always gives me a rect about 203mm. Also changing the size of the rect item gives me always the same result size.

    What I found is that changing the scene rect also changes the resulting size, so
    Qt Code:
    1. pqScene->setSceneRect(0,0,1000,1000);
    To copy to clipboard, switch view to plain text mode 
    Gives me a rect that is 76,6mm

    Can anyone explain me what I'm not understanding?

    Thanks,
    Òscar Llarch i Galán

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,229
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: GraphicsScene print

    Your view (ui.graphicsView) is on screen, right? I think you have a mismatch between the screen resolution (75 DPI maybe) and size of the view and your printer resolution and page size, as well as possibly not taking into account printer margins. Is your view also calling fitInView() in the resize method?

    What happens if you render the view rather than the scene? (ui.graphicsView.render( &qPainter ))
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  3. #3
    Join Date
    Jan 2006
    Location
    Sta. Eugènia de Berga (Vic - Barcelona - Spain)
    Posts
    869
    Thanks
    70
    Thanked 59 Times in 57 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: GraphicsScene print

    Your view (ui.graphicsView) is on screen, right?
    Yes.

    I think you have a mismatch between the screen resolution (75 DPI maybe) and size of the view and your printer resolution and page size, as well as possibly not taking into account printer margins
    This is what I don't understand how it works. The screen resolution returns 96 DPI. My printer will use 203 DPI but for testing purposes I'm exporting it to PDF with 300 DPI by now.

    Is your view also calling fitInView() in the resize method?
    I have added it now with same result.

    What happens if you render the view rather than the scene? (ui.graphicsView.render( &qPainter ))
    This gives me a size of 36,84mm.


    What I have addeed now and seems to work is:
    Qt Code:
    1. //Adding items
    2. int iScreenDPIs = logicalDpiX(); //96 DPI
    3. qreal qScreenResolutionMmPx = 1 / ((double)iScreenDPIs / 25.4);
    4. pqScene->setSceneRect(0,0,210/qScreenResolutionMmPx,297/qScreenResolutionMmPx);
    5. pqScene->addRect(50/qScreenResolutionMmPx,50/qScreenResolutionMmPx,100/qScreenResolutionMmPx,100/qScreenResolutionMmPx,QPen(Qt::blue));
    6.  
    7. //Add text item to see if it mantain the font "point size"
    8. QFont qFont("Arial",10);
    9. QGraphicsItemText *pqTextItem = pqScene->addText("TEST"·,qFont);
    10. pqTextItem->setPos(50/qResolutionMmPx,50/qResolutionMmPx);
    11.  
    12. //Rendering
    13. int iPrinterDPIs = 300; //Desired printing resolution
    14. qreal qPrinterResolutionMmPx = 1 / ((double)iPrinterDPIs / 25.4);
    15. pqScene->render(&qPainter,QRectF(0,0,210/qPrinterResolutionMmPx ,297/qPrinterResolutionMmPx )); //Render the scene scaled to the desired size
    To copy to clipboard, switch view to plain text mode 
    As it seems to adapt the scene to the deisred rect.

    I really do not fully uderstand why it renders right.

    I need to print some images and texts that the user can position, change font family and size, ... And the firts problem that I found was when adding the text item as it takes the screen DPIs to create.
    Òscar Llarch i Galán

  4. #4
    Join Date
    Jan 2006
    Location
    Sta. Eugènia de Berga (Vic - Barcelona - Spain)
    Posts
    869
    Thanks
    70
    Thanked 59 Times in 57 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: GraphicsScene print

    Well,

    First of all,
    Qt Code:
    1. int iScreenDPIs = logicalDpiX(); //96 DPI
    2. qreal qScreenResolutionMmPx = 1 / ((double)iScreenDPIs / 25.4);
    3. pqScene->setSceneRect(0,0,210/qScreenResolutionMmPx,297/qScreenResolutionMmPx);
    To copy to clipboard, switch view to plain text mode 

    We need to set the scene rect with a mm scale because when adding a QGraphicsTextItem to the secene
    Qt Code:
    1. QFont qFont("Arial",10);
    2. QGraphicsItemText *pqTextItem = pqScene->addText("TEST"·,qFont);
    To copy to clipboard, switch view to plain text mode 
    it takes the screen DPI to calculate the size of the item. This is ok, calculating the boundingRect of the item and using this DPI gets me the real size of a printed text in Arial 10pt.

    So, finally, when printing, the "render" method "maps from the source rectangle, in scene units, to a target rectangle, in device units", extracted from http://stackoverflow.com/questions/3...-inch-mm-scale

    and is for that reason that I have to calculate the destination rectangle taking the printer resolution.

    Think on a CAD software for example. You can draw a rect that represents a 10x10m and you can print it as 20x20mm using the printer resolution and a scale factor.
    Òscar Llarch i Galán

  5. The following user says thank you to ^NyAw^ for this useful post:

    d_stranz (11th January 2017)

Similar Threads

  1. Problem on Print GraphicsScene with OpenGL
    By redmong in forum Qt Programming
    Replies: 0
    Last Post: 2nd June 2010, 16:37
  2. transfer graphicsscene on form1 to graphicsscene on form2
    By rogerholmes in forum Qt Programming
    Replies: 2
    Last Post: 25th September 2009, 21:37
  3. Cancelling Long-running Print/Print Preview
    By ChrisW67 in forum Newbie
    Replies: 4
    Last Post: 17th June 2009, 00:05
  4. Line in GraphicsScene
    By konvex in forum Qt Programming
    Replies: 6
    Last Post: 16th November 2008, 13:53
  5. GraphicsScene Question
    By QbelcorT in forum Qt Programming
    Replies: 3
    Last Post: 19th September 2008, 09:03

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.