title: How to save a treeview as pdf in PySide/PyQt?
body: <p><strong>Short version</strong></p>
  
  <p>I have a simple <code>QTreeView</code> embedded in a widget:</p>
  
  <p><img src="https://i.stack.imgur.com/Oqnu7.png" alt="the gui"></p>
  
  <p>I want to save the tree as a PDF so users can print it. How can I do this? </p>
  
  <p><strong>Details</strong></p>
  
  <p>I want to save the tree as a PDF when I click the 'print' button. Based on <a href="https://stackoverflow.com/questions/14735089/tableview-to-pdf-several-pages">this question</a>, and somewhat on <a href="https://wiki.qt.io/Handling_PDF" rel="nofollow noreferrer">Qt documentation</a>, I have tried doing it with the following method (see full SSCCE below for context):</p>
  
  <pre><code>def print_(self): 
      printer = QtGui.QPrinter(QtGui.QPrinter.HighResolution)
      printer.setPageSize(QtGui.QPrinter.Letter)
      printer.setOutputFormat(QtGui.QPrinter.PdfFormat)
      printer.setOutputFileName("treeTest.pdf")
      painter = QtGui.QPainter()
      painter.begin(printer)
      self.view.render(painter)
      painter.end()
  </code></pre>
  
  <p>But when this method is run, I get an error:</p>
  
  <pre><code>TypeError: 'PySide.QtGui.QWidget.render' called with wrong argument types.  
  PySide.QtGui.QWidget.render(PySide.QtGui.QPainter)
  </code></pre>
  
  <p>Curiously, <a href="http://srinikom.github.io/pyside-docs/PySide/QtGui/QWidget.html#PySide.QtGui.PySide.QtGui.QWidget.render" rel="nofollow noreferrer">the documentation for render</a> has the first argument as a <code>QPainter</code> type, so I am not sure why my code isn't working, and especially am confused about the type error.</p>
  
  <p>Am I missing some simple two-liner that will let me export my tree as a PDF?</p>
  
  <p><strong>SSCCE</strong></p>
  
  <pre><code>from PySide import QtGui, QtCore
  import sys
  
  class MyTreeView(QtGui.QWidget):
      def __init__(self, parent = None):
          QtGui.QWidget.__init__(self, parent = None)
          self.setAttribute(QtCore.Qt.WA_DeleteOnClose)
          self.view = QtGui.QTreeView(self)
          self.createModel()
          self.view.setModel(self.model)
          buttonLayout = self.buttonSetup()
          self.makeConnections()
          mainLayout = QtGui.QHBoxLayout(self)
          mainLayout.addWidget(self.view)
          mainLayout.addLayout(buttonLayout)
          self.setLayout(mainLayout)
          self.view.expandAll()
  
      def createModel(self):
          self.model = QtGui.QStandardItemModel()
          self.model.setHorizontalHeaderLabels(['Task', 'Comment'])
          self.rootItem = self.model.invisibleRootItem()
          #First top-level row and children 
          item0 = [QtGui.QStandardItem('Coding fun'), QtGui.QStandardItem('Make millions')]
          item00 = [QtGui.QStandardItem('Write example'), QtGui.QStandardItem('Keep it simple')]
          item01 = [QtGui.QStandardItem('Post to SO'), QtGui.QStandardItem('Put on flame-retardant vest')]
          item00[0].setCheckable(True)
          item00[0].setCheckState(QtCore.Qt.Unchecked)
          item01[0].setCheckable(True)
          item01[0].setCheckState(QtCore.Qt.Unchecked)
          self.rootItem.appendRow(item0)
          item0[0].appendRow(item00)
          item0[0].appendRow(item01)
  
      def makeConnections(self):
          self.printButton.clicked.connect(self.print_)
          self.quitButton.clicked.connect(self.close)
  
      def print_(self):
          printer = QtGui.QPrinter(QtGui.QPrinter.HighResolution)
          printer.setPageSize(QtGui.QPrinter.Letter)
          printer.setOutputFormat(QtGui.QPrinter.PdfFormat)
          printer.setOutputFileName("treeTest.pdf")
          painter = QtGui.QPainter()
          painter.begin(printer)
          self.view.render(painter)
          painter.end()
  
      def buttonSetup(self):
          self.printButton = QtGui.QPushButton("Print")
          self.quitButton = QtGui.QPushButton("Quit")
          #Lay them out
          buttonLayout = QtGui.QVBoxLayout()
          buttonLayout.addStretch()
          buttonLayout.addWidget(self.printButton)
          buttonLayout.addStretch()
          buttonLayout.addWidget(self.quitButton)
          return buttonLayout
  
  
  def main():
      app = QtGui.QApplication(sys.argv)
      newTree = MyTreeView()
      newTree.show()
      sys.exit(app.exec_())
  
  
  if __name__ == "__main__":
      main()
  </code></pre>