KDE TechBase
  • Page
  • Discussion
  • Edit
  • History
KDE TechBase is a Wiki - You can help! Please contribute! Questions?

Development/Tutorials/Printing Hello World

< Development | Tutorials
Note
noframe
This tutorial seems not to be up to date with current KDE4 development.


Contents

  • 1 The mission
  • 2 The KDE version
  • 3 The code
  • 4 Explanation
  • 5 How to compile
    • 5.1 With KDE 4 and CMake
      • 5.1.1 CMakeLists.txt
      • 5.1.2 Make and Run
    • 5.2 With KDE 3
  • 6 See also

[edit] The mission

Print Hello World on your printer.

[edit] The KDE version

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

[edit] The code

  1. #include <kprinter.h>
  2. #include <qpainter.h>
  3. #include <kapplication.h>
  4. #include <kaboutdata.h>
  5. #include <kmessagebox.h>
  6. #include <kcmdlineargs.h>
  7.  
  8. /*
  9. This prints Hello World on your printer
  10. */
  11.  
  12. int main(int argc, char *argv[])
  13. {
  14. KAboutData aboutData( "test", "test", "1.0", "test",
  15. KAboutData::License_GPL, "(c) 2006" );
  16. KCmdLineArgs::init( argc, argv, &aboutData );
  17. KApplication app;
  18.  
  19. KPrinter job;
  20. job.setFullPage( true );
  21. if ( job.setup() )
  22. {
  23. QPainter painter;
  24. painter.begin( &job );
  25. painter.drawText(100,100,"Hello World");
  26. painter.end();
  27. // this makes the print job start
  28. }
  29. }

[edit] 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.

[edit] How to compile

[edit] With KDE 4 and CMake

[edit] CMakeLists.txt

find_package(KDE4 REQUIRED)
include_directories( ${KDE4_INCLUDES} )
kde4_add_executable(printhelloworld printtest.cpp)
target_link_libraries(printhelloworld ${KDE4_KDEPRINT_LIBS})

[edit] Make and Run

Then do:

cmake .
make
./printhelloworld

[edit] 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

[edit] See also

  • Point your konqueror to kde:kprinter
Retrieved from "http://techbase.kde.org/Development/Tutorials/Printing_Hello_World"

Categories: KDE4 | C++

Navigation

  • Home
  • Help
  • Recent changes

Sections

  • Getting started
  • Development
  • Schedules
  • Policies
  • Contribute
  • Projects

Toolbox

  • What links here
  • Related changes
  • Upload file
  • Special pages
  • Printable version
  • Permanent link

Personal tools

  • Log in / create account
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. Qt® and Trolltech® are registered trademarks of Trolltech ASA. Linux® is a registered Trademark of Linus Torvalds. | Legal