#!/usr/bin/env python """ This is more of a manuall technique to print a widget """ from PyQt4.QtGui import * from PyQt4.QtCore import * #!/usr/bin/env python from PyQt4 import QtWebKit import sys class testWidget(QWidget): """this class loads an image and then lets you print it to a file, or print preview, the print preview will give you all of you currently configured print options. First it loads a file creates a label then it sets a regular printer, associates that print to a buttonA. Next it sets the print preview to a buttonB.""" def __init__(self,ifile, parent=None): QWidget.__init__(self,parent) self.myimage = QPixmap(ifile) self.setLayout(QHBoxLayout()) if self.myimage.isNull(): self.mylabel = QLabel('Missing Image',self) else: self.mylabel = QLabel(self) self.mylabel.setPixmap(self.myimage) self.layout().addWidget(self.mylabel) self.myprinter = QPrinter() self.myprinter.setOutputFormat(QPrinter.PdfFormat); self.myprinter.setOutputFileName("nonwritable.pdf"); self.buttonA = QPushButton("print",self) self.layout().addWidget(self.buttonA) self.connect(self.buttonA,SIGNAL("clicked ()"), self.nopreview) self.preview = QPrintPreviewDialog() self.connect(self.preview,SIGNAL("paintRequested (QPrinter *)"),self.what) self.buttonB = QPushButton("preview",self) self.layout().addWidget(self.buttonB) self.connect(self.buttonB,SIGNAL("clicked ()"), self.ppreview) def what(self,other): painter = QPainter() painter.begin(other) self.mylabel.render(painter) painter.end() def ppreview(self): print "working" self.preview.exec_() def nopreview(self): print "working" self.what(self.myprinter) if __name__=="__main__": app = QApplication(sys.argv) fileName = QFileDialog.getOpenFileName(None, "Open Image", ".", "Image Files (*.png *.jpg *.bmp)") widget = testWidget(fileName) widget.show() sys.exit(app.exec_())