[PyQt] QPrintDialog (pyqt4)

Russell Valentine russ at coldstonelabs.org
Thu Feb 18 09:00:44 GMT 2010


Joi Barnett wrote:
> thank you for the reply,
> 
> do you have an example of how to use QPrinter in python? 


I didn't have one, but I just made one really quick. Very simple 
example. I Cc'd the list in case anyone else had anything to add.


from PyQt4 import QtGui, QtCore

class MainWindow(QtGui.QMainWindow):
     def __init__(self, app):
         QtGui.QMainWindow.__init__(self)
         self.app=app
         self.label=QtGui.QLabel("Print test")
         self.setCentralWidget(self.label)
     def goPrinter(self):
         printer=QtGui.QPrinter()
         dialog = QtGui.QPrintDialog(printer, self)
         if(dialog.exec_() != QtGui.QDialog.Accepted):
             return
         printLabel = QtGui.QLabel("Hello my printer.")
         painter = QtGui.QPainter(printer)
         printLabel.render(painter)
         painter.end()

if __name__ == "__main__":
     import sys
     app = QtGui.QApplication(sys.argv)
     window=MainWindow(app)
     window.show()
     QtCore.QTimer.singleShot(1000, window.goPrinter)
     #ui.show()
     sys.exit(app.exec_())



More information about the PyQt mailing list