title: I need to print a QGraphicsScene to actual (inch/mm) scale
body: <p>I am trying to print the contents of a <code>QGraphicsScene</code>. The target printer could be anything - from the normal printers to custom size special printers. It must print things at actual size (inches, mm....).<br>
  In the <code>QGraphicsScene</code> I am using the assumption of 72 ppi.</p>
  
  <p>I assumed that:<br>
  1) rendering the scene to a printer would do it based on printer resolution, so that I would get items at actual size (inches/mm) similar to what they show on screen.<br>
  2) I can set the paper size for the printer to the desired canvas size (which is a rectangle on a very large scene) and nothing beyond it would print<br>
  3) I can set margins, and the contents outside the "actual canvas" will not be printed, including what is on margins.</p>
  
  <p>All my assumptions so far are wrong:<br>
  1) for different printers, it seems the rendering is for max fit (using aspect ratio), if I suggest a custom size close to its default paper size (or if I don't set a paper size);<br>
  If I set a paper size that is not close (like 4x4 inch on a printer with default "LETTER" size) it just prints a blank page.<br>
  2-3) In the case where there is a print, and the printer just stretches the canvas to its full page, any items that are outside the drawing area are still printed.<br>
  I tried to clip, either on the painter or by setting the target rectangle on render, and the result was very odd clipping of a small section of the scene.</p>
  
  <p>I have tried on HP LaserJet, Adobe PDF, and some custom printers with specific sizes (like 4x6 inch). They all scale the scene to the max size based on whether I specify Portrait or Landscape, and completely ignore my paper size request or the actual sizes.</p>
  
  <p>Here is a small sample program to reproduce what I am trying to do.<br>
  The comments in the code show some options I tried.</p>
  
  <pre><code>#include &lt;QApplication&gt;
  #include &lt;QGraphicsView&gt;
  #include &lt;QGraphicsScene&gt;
  #include &lt;QGraphicsRectItem&gt;
  #include &lt;QGraphicsEllipseItem&gt;
  #include &lt;QPrinter&gt;
  #include &lt;QPrintDialog&gt;
  
  
  int main(int argc, char *argv[])
  {
      QApplication app(argc, argv);
      QGraphicsScene* s = new QGraphicsScene();
      s-&gt;setSceneRect(-500, -500, 1500, 1500);
      QGraphicsView* view = new QGraphicsView();
      view-&gt;setScene(s);
      view-&gt;show();
  
      int canvasSize = 288;    // 4 in
      QRectF canvasRect(0, 0, canvasSize, canvasSize);
      // this is to show actual scene
      QGraphicsRectItem* sss = new QGraphicsRectItem(canvasRect);
      sss-&gt;setBrush(Qt::blue);
      s-&gt;addItem(sss);
      // this item is partially outside top left
      QGraphicsEllipseItem* e1 = new QGraphicsEllipseItem(-50, -75, 100, 150);
      e1-&gt;setBrush(Qt::yellow);
      s-&gt;addItem(e1);
      // this item is partially outside center
      QGraphicsEllipseItem* e2 = new QGraphicsEllipseItem(100, 150, 250, 50);
      e2-&gt;setBrush(Qt::yellow);
      s-&gt;addItem(e2);
      // this item is partially outside right
      QGraphicsEllipseItem* e3 = new QGraphicsEllipseItem(200, 200, 75, 125);
      e3-&gt;setBrush(Qt::yellow);
      s-&gt;addItem(e3);
  
      QPrinter printer;
      // QPrinter printer(QPrinter::HighResolution);  // this makes no difference except it rotates the output, strange
  
      // without this just to use default printer, if you like
      QPrintDialog printDialog(&amp;printer);
      if (printDialog.exec() != QDialog::Accepted)
          return 1;
  
      printer.setFullPage(false); // I see no diference between true and false
  
      // this results in empty page (or is ignored if my rect is 8 in)
      //printer.setPaperSize(canvasRect, QPrinter::Point);
  
      printer.setOrientation(QPrinter::Landscape);
      printer.setPageMargins(0, 0, 0, 0, QPrinter::Point);
  
      QPainter painter;
  
      if (painter.begin(&amp;printer))
      {
  //        painter.setClipRect(canvasRect);  // this creates a small clipping, only a tiny corner
          s-&gt;render(&amp;painter, QRectF(), canvasRect, Qt::KeepAspectRatio);
          // doing this instead clips to a tiny rectangle also
  //        s-&gt;render(&amp;painter, canvasRect, canvasRect, Qt::KeepAspectRatio);
          painter.end();
      }
  
      return app.exec();
  }
  </code></pre>
  
  <p>Doing:</p>
  
  <pre><code>QPrinter printer(QPrinter::HighResolution);
  qreal resolutionFactor = printer.resolution() / 1200.;
  ...
  painter.scale(resolutionFactor, resolutionFactor);
  </code></pre>
  
  <p>fixes the LaserJet print (the scaling - not the painting outside the actual page) - but results into a tiny almost invisible print on a printer with 300 dpi resolution.</p>
  
  <p>How can I get the printed output to be to actual scale (so that I can measure inches/mm on paper and have them be correct) ?</p>
  
  <p>Also how can I get the output to be clipped to the actual canvas rectangle ?</p>