Adding a file menu entry and a toolbar button for the print functionality is easy enough. We don't need to have a separate printer setup and print dialog. We are going to show the printing setup dialog every time a user wants to print a document. Because we use one central QPrinter object, settings remain valid for the duration of the application. You might want to save the printer settings to the configuration file (I haven't done that here).
# fragment from kalamapp.py def initActions(self): self.actions = {} ... self.actions["filePrint"] = QAction("Print", QIconSet(QPixmap(fileprint)), "&Print", QAccel.stringToKey("CTRL+P"), self) self.connect(self.actions["filePrint"], SIGNAL("activated()"), self.slotFilePrint) ... def initMenuBar(self): self.fileMenu = QPopupMenu() ... self.actions["filePrint"].addTo(self.fileMenu) ... self.menuBar().insertItem("&File", self.fileMenu) ... def initToolBar(self): self.fileToolbar = QToolBar(self, "file operations") ... self.actions["filePrint"].addTo(self.fileToolbar) ... def setActionsEnabled(self, *args): enabled = self.docManager.numberOfDocuments() ... self.actions["filePrint"].setEnabled(enabled) ... def slotFilePrint(self): if self.printer.setup(): print "Printing" ...
The printer setup dialog is shown whenever the slotFilePrint method is called. Printing will commence when the user presses "OK."