In this chapter we will show you how easy it is to implement printing functions using Qt. It´s actually just one line of code for us todo here, but we will start understanding who is actually doing the printing job. When the user presses the print button inKScribble or chooses "Print" from the "File" menu, the slotFilePrint()
method is called in KScribbleApp
.This method detects which child window is currently activ and creates a printer instance of the class QPrinter
. Then it callsthe widget´s printing method, KScribbleView::print()
. Here, the framework already contains the base implementation - whichalready shows you that for printing you just have to use QPainter
which then draws on the printer. This method also calls theprinting dialog.What we have to do here is to use QPainter
methods to draw the pixmap of the document connected to the view. AsQPainter
already offers a whole set of methods drawPixmap()
, we will of course use one of them:
void KScribbleView::print(QPrinter *pPrinter){ if (pPrinter->setup(this)) { QPainter p; p.begin(pPrinter); /////////////////////////////// // TODO: add your printing code here-> p.drawPixmap(0,0,doc->buffer); /////////////////////////////// p.end(); }}
tar zxvf kscribble-1.0.tar.gz
, load the project and call "Automake and autoconf" from the"Build" menu in KDevelop, then call "./configure" from the same menu. The configure options are those of my installtion of the KDE 2and Qt 2.1, so you have to change them manually to match your installation path for these options.The appendix also contains the complete sourcecode for this package to read through online.