[PyQt] Printing a widget

Robert J. Hansen rjh at sixdemonbag.org
Thu Jul 30 18:58:59 BST 2009


For a piece of code I'm working on, I need the ability to render a  
QWidget to the printer directly.  The logic goes something like this:

def printout(self):
     widget = self.tab_widget.currentWidget()
     if widget == None:
         # The 'print' button should've been disabled;
         # do so now.
         self.update_ui()
         return
     try:
         widget.printout()
     except:
         painter = QPainter(self.get_printer())
         rectangle = painter.viewport()
         size = widget.size()
         size.scale(rectangle.size(), Qt.KeepAspectRatio)
         painter.setViewport(rectangle.x(), rectangle.y(),
             size.width(), size.height())
         widget.render(painter)

... Unfortunately, this code does not seem to work.  Something gets  
printed, but it's such a tiny postage stamp that it's not usable for  
my purposes.  For my tests the widget has been a QLabel; the 'except'  
block is executed.

What I'm looking for is some way to print such that the widget's width  
spans the width of the page, and its length will be split up among  
multiple pages if necessary.  If anyone can give me the magic  
invocation that will do this, or can direct me to the proper part of  
the PyQt documentation that tells how to do this, I would be deeply  
appreciative.  Thank you!





More information about the PyQt mailing list