title: How to print a QQuickView's contents to PDF?
body: <p>I'm upgrading some Qt (C++ &amp; QML) code from Qt4.8 to Qt5.1.</p>
  
  <p>The Qt4.8 code is a trivial C++ "QML viewer" app subclassing a <code>QDeclarativeView</code>, and a bunch of QML.</p>
  
  <p>It's been easy enough to change this to use Qt5/QtQuick2's <code>QQuickView</code> except for one thing:</p>
  
  <p>The Qt4.8 app has a method for printing to PDF:</p>
  
  <pre><code>void MyQMLViewer::printToPDF(const QString&amp; filename) const {
    QPrinter printer(QPrinter::HighResolution);
    printer.setOutputFormat(QPrinter::PdfFormat);
    printer.setPageSize(QPrinter::A3);
    printer.setOutputFileName(filename);
    printer.setOrientation(QPrinter::Landscape);
    QPainter painter(&amp;printer);
    render(&amp;painter);
  }
  </code></pre>
  
  <p>There were a few "environment" changes needed for Qt5.1 to get a <code>QPrinter</code> (ie add <code>QT += printsupport</code> to the project .pro file and <code>#include &lt;QtPrintSupport&gt;</code>), but there seems to be a more fundamental problem that <code>QQuickView</code> doesn't provide anything which is obviously compatible with the <code>QGraphicsView</code>/<code>QPainter</code>/<code>QPaintDevice</code> world of <code>QPrinter</code> (specifically, <code>QQuickView</code> has no <code>render</code> method, and all the drawing/painting/rendering-related methods it does have seem very tied up with OpenGL).</p>
  
  <p>Any suggestions for how to best obtain high-quality PDF output from a <code>QQuickView</code> ?</p>
  
  <p>(Note that I am <code>not</code> simply looking to screenshot the view; with <code>QDeclarativeView</code>, the code above generates PDFs with <em>much</em> better resolution even than the app fullscreened on my largest monitor).</p>
  
  <p>I see the "QDeclarativeItem and QDeclarativeView" section of the <a href="http://qt-project.org/doc/qt-5.0/qtquick/qtquick-porting-qt5.html#qdeclarativeitem-and-qdeclarativeview" rel="nofollow noreferrer">"Porting QML Applications to Qt 5"</a> guide does mention the loss of <code>QGraphicsView</code>-specific functionality, but doesn't offer any solutions (although it does mention workrounds for the case of items with custom <code>QPainter</code>-based rendering being bought into the new regime).</p>
  
  <p><strong>Update</strong> with some additional background info: an example of a PDF printed from QDeclarativeView using the above code can be found <a href="https://bitbucket.org/timday/qtlab/downloads/so-20131230-slide19.pdf" rel="nofollow noreferrer">here</a>.  There's a png of the same view on a decent size monitor <a href="https://bitbucket.org/timday/qtlab/downloads/so-20131230-slide19.png" rel="nofollow noreferrer">here</a>.  (This is actually the last slide in a series of slides; it's actually a gallery of the previous slides which bounces each slide onto the screen; if I had the time I'd look into the feasibility of the gallery being the <em>only</em> thing and transforming each scattered slide into view for a <a href="http://en.wikipedia.org/wiki/Prezi" rel="nofollow noreferrer">Prezi</a>-style presentation; suspect QDeclarative isn't really performant enough though, which is one reason for wanting to get onto QtQuick2+Qt5.2's new scene graph stuff).  Anyway, If you zoom the PDF up to 100% you'll see the text is... well it's better than anything a sanely sized image file will manage I think, although the sloping text baselines perhaps look a little uneven.  There is also an issue with opacity values not being represented in the PDF (so the drop shadows and "bubbles" come out solid); another one of my motivations for trying for a QtQuick2 version was actually to see if translucent elements were dealt with any better.  I assume the PDF just contains rasterized (or maybe vector outlines) of all the elements as utilities like "pdftotext" can't extract anything from it.  I don't know enough about PDF tools to know how to inspect the internal structure of the thing but I assume there's some hierarchy there and the QML element tree is all laid out using a similar structure of nested transforms to the QML.  Just for comparison and the sort of richness I'm potentially working towards <a href="http://timday.bitbucket.org/tomographylab-poster.pdf" rel="nofollow noreferrer">here's</a> a poster I did with LaTeX/Beamerposter; I find Beamerposter's rigid block structure rather limiting (and <a href="https://tex.stackexchange.com/questions/74808/how-to-line-up-blocks-in-columns-with-full-width-blocks-in-beamer-beamerposter">fiddly</a>) compared with the possibilities QML seems to offer though.  BTW, another thing on my wishlist/todolist is a <a href="https://stackoverflow.com/questions/13568233/possibilities-to-embed-latex-in-qml">QML element which can render LaTeX source</a>, math and all, just to get the best of both worlds.</p>
  
  <p><strong>Update</strong>: Recent <a href="http://blog.qt.io/blog/2016/08/15/the-qt-quick-graphics-stack-in-qt-5-8/" rel="nofollow noreferrer">Qt blog post</a> on all the backend changes in Qt5.8 has a comment linking to <a href="https://bugreports.qt.io/browse/QTBUG-55321" rel="nofollow noreferrer">this Qt issue</a> to use the new possibility of QPainter-rendered QtQuick scenes to render PDFs.</p>