Difference between revisions of "Development/Tutorials/Printing Hello World"

Jump to: navigation, search
(sorry, sorry)
Line 1: Line 1:
[[Programming Tutorials/printing/KDE4|Printing in KDE 4]]
+
=The mission=
 +
Print '''Hello World''' on your printer.
 +
 
 +
=The KDE version=
 +
This code will work for KDE 3 as for KDE 4.
 +
 
 +
=The code=
 +
<highlightSyntax language="cpp">
 +
#include <kprinter.h>
 +
#include <qpainter.h>
 +
#include <kapplication.h>
 +
#include <kaboutdata.h>
 +
#include <kmessagebox.h>
 +
#include <kcmdlineargs.h>
 +
 
 +
/*
 +
This prints Hello World on your printer
 +
*/
 +
 
 +
int main(int argc, char *argv[])
 +
{
 +
        KAboutData aboutData( "test", "test",
 +
        "1.0", "test", KAboutData::License_GPL,
 +
        "(c) 2006" );
 +
        KCmdLineArgs::init( argc, argv, &aboutData );
 +
        KApplication khello;
 +
 
 +
  KPrinter job;
 +
  job.setFullPage( true );
 +
  if ( job.setup() )
 +
  {
 +
    QPainter painter;
 +
    painter.begin( &job );
 +
    painter.drawText(100,100,"Hello World");
 +
    painter.end();
 +
    // this makes the print job start
 +
  }
 +
}
 +
</highlightSyntax>
 +
 
 +
=Explanation=
 +
You need a KDE instance to print, because this instance stores your configuration, including your printer configuration. You create one by instanciating the KApplication class.
 +
 
 +
=How to compile=
 +
==With KDE 4==
 +
Quite complicated:
 +
gcc print.cpp -o print -I/home/kde-devel/qt-unstable/include/Qt \
 +
-I/home/kde-devel/qt-unstable/include/Qt-Core \
 +
-I/home/kde-devel/qt-unstable/include -I/home/kde-devel/kde/include \
 +
-L/home/kde-devel/kde/lib -L/home/kde-devel/qt-unstable/lib -lkdeui \
 +
-lkdecore -ldl -lkdeprint
 +
 
 +
==With KDE 3==
 +
Quite easy:
 +
gcc printtest.cpp -o print -I/usr/lib/qt3/include -I/opt/kde3/include \
 +
-L/opt/kde3/lib -L/usr/lib/qt3/lib  -lqt-mt -lkdeprint
 +
 
 +
=See also=
 +
* Point your konqueror to [http://developer.kde.org/documentation/library/cvs-api/kdelibs-apidocs/kdeprint/html/classKPrinter.html kde:kprinter]

Revision as of 22:31, 26 September 2006

Contents

The mission

Print Hello World on your printer.

The KDE version

This code will work for KDE 3 as for KDE 4.

The code

<highlightSyntax language="cpp">

  1. include <kprinter.h>
  2. include <qpainter.h>
  3. include <kapplication.h>
  4. include <kaboutdata.h>
  5. include <kmessagebox.h>
  6. include <kcmdlineargs.h>

/* This prints Hello World on your printer

  • /

int main(int argc, char *argv[]) {

       KAboutData aboutData( "test", "test",
       "1.0", "test", KAboutData::License_GPL,
       "(c) 2006" );
       KCmdLineArgs::init( argc, argv, &aboutData );
       KApplication khello;
 KPrinter job;
 job.setFullPage( true );
 if ( job.setup() )
 {
   QPainter painter;
   painter.begin( &job );
   painter.drawText(100,100,"Hello World");
   painter.end(); 
   // this makes the print job start
 }

} </highlightSyntax>

Explanation

You need a KDE instance to print, because this instance stores your configuration, including your printer configuration. You create one by instanciating the KApplication class.

How to compile

With KDE 4

Quite complicated:

gcc print.cpp -o print -I/home/kde-devel/qt-unstable/include/Qt \
-I/home/kde-devel/qt-unstable/include/Qt-Core \
-I/home/kde-devel/qt-unstable/include -I/home/kde-devel/kde/include \
-L/home/kde-devel/kde/lib -L/home/kde-devel/qt-unstable/lib -lkdeui \
-lkdecore -ldl -lkdeprint

With KDE 3

Quite easy:

gcc printtest.cpp -o print -I/usr/lib/qt3/include -I/opt/kde3/include \
-L/opt/kde3/lib -L/usr/lib/qt3/lib  -lqt-mt -lkdeprint

See also


KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V.Legal