Development/Tutorials/Printing Hello World: Difference between revisions

From KDE TechBase
(Undo revision 38831 by Midnighter (Talk))
No edit summary
Line 1: Line 1:
{{note|This tutorial seems not to be up to date with current KDE4 development.}}
== The mission ==


=The mission=
Print '''Hello World''' on your printer.
Print '''Hello World''' on your printer.


=The KDE version=
== The code ==
This code will work for KDE 3 as for KDE 4.
 
=The code=
<code cppqt n>
<code cppqt n>
#include <kprinter.h>
#include <qpainter.h>
#include <qpainter.h>
#include <qprinter.h>
#include <kapplication.h>
#include <kapplication.h>
#include <kaboutdata.h>
#include <kaboutdata.h>
Line 22: Line 18:
int main(int argc, char *argv[])
int main(int argc, char *argv[])
{
{
  KAboutData aboutData( "test", "test", "1.0", "test",
    KAboutData aboutData( "test", "test", "1.0", "test",
      KAboutData::License_GPL, "(c) 2006" );
                          KAboutData::License_GPL, "(c) 2006" );
  KCmdLineArgs::init( argc, argv, &aboutData );
    KCmdLineArgs::init( argc, argv, &aboutData );
  KApplication app;
    KApplication app;


  KPrinter job;
    QPrinter printer;
  job.setFullPage( true );
    printer.setFullPage(true);
  if ( job.setup() )
  {
     QPainter painter;
     QPainter painter;
     painter.begin( &job );
     painter.begin(&printer);
     painter.drawText(100,100,"Hello World");
     painter.drawText(100,100,"Hello World");
     painter.end();  
     painter.end();  
     // this makes the print job start
     // this makes the print job start
  }
}
}
</code>
</code>


=Explanation=
== How to compile ==
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.
 
=== CMakeLists.txt ===


=How to compile=
==With KDE 4 and CMake==
===CMakeLists.txt===
<code>
<code>
find_package(KDE4 REQUIRED)
find_package(KDE4 REQUIRED)
include_directories( ${KDE4_INCLUDES} )
include_directories( ${KDE4_INCLUDES} )
kde4_add_executable(printhelloworld printtest.cpp)
kde4_add_executable(printhelloworld printtest.cpp)
target_link_libraries(printhelloworld ${KDE4_KDEPRINT_LIBS})
target_link_libraries(printhelloworld ${${KDE4_KDECORE_LIBS} ${QT_QTGUI_LIBRARY})
</code>
</code>
===Make and Run===
 
=== Make and Run ===
 
Then do:
Then do:
  cmake .
  cmake .
Line 58: Line 51:
  ./printhelloworld
  ./printhelloworld


==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]
[[Category:KDE4]]
[[Category:KDE4]]
[[Category:C++]]
[[Category:C++]]

Revision as of 12:11, 27 May 2011

The mission

Print Hello World on your printer.

The code

  1. include <qpainter.h>
  2. include <qprinter.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 app;
   QPrinter printer;
   printer.setFullPage(true);
   QPainter painter;
   painter.begin(&printer);
   painter.drawText(100,100,"Hello World");
   painter.end(); 
   // this makes the print job start

}

How to compile

CMakeLists.txt

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

Make and Run

Then do:

cmake .
make
./printhelloworld