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

Jump to: navigation, search
(sorry, sorry)
(Next)
 
(23 intermediate revisions by 9 users not shown)
Line 1: Line 1:
=The mission=
+
== The mission ==
Print '''Hello World''' on your printer.
+
  
=The KDE version=
+
Print '''Hello World''' on your printer. There will be no more dialog to confirm how many pages you want or the like.
This code will work for KDE 3 as for KDE 4.
+
  
=The code=
+
== The files ==
<highlightSyntax language="cpp">
+
'''main.cpp'''
#include <kprinter.h>
+
<syntaxhighlight lang="cpp-qt" line>
 
#include <qpainter.h>
 
#include <qpainter.h>
 +
#include <qprinter.h>
 
#include <kapplication.h>
 
#include <kapplication.h>
 
#include <kaboutdata.h>
 
#include <kaboutdata.h>
Line 20: Line 19:
 
int main(int argc, char *argv[])
 
int main(int argc, char *argv[])
 
{
 
{
        KAboutData aboutData( "test", "test",
+
    KAboutData aboutData(
        "1.0", "test", KAboutData::License_GPL,
+
                        // The program name used internally.
        "(c) 2006" );
+
                        "tutorial-printing",
        KCmdLineArgs::init( argc, argv, &aboutData );
+
                        // The message catalog name
        KApplication khello;
+
                        // If null, program name is used instead.
 +
                        0,
 +
                        // A displayable program name string.
 +
                        ki18n("Printing Tutorial"),
 +
                        // The program version string.
 +
                        "1.0",
 +
                        // Short description of what the app does.
 +
                        ki18n("Displays a KMessageBox popup"),
 +
                        // The license this code is released under
 +
                        KAboutData::License_GPL,
 +
                        // Copyright Statement
 +
                        ki18n("(c) 2006-2011"),
 +
                        // Optional text shown in the About box.
 +
                        // Can contain any information desired.
 +
                        ki18n("Some text..."),
 +
                        // The program homepage string.
 +
                        "http://example.com/",
 +
                        // The bug report email address
 +
                        "submit@bugs.kde.org");
 +
    KCmdLineArgs::init( argc, argv, &aboutData );
 +
    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
  }
 
 
}
 
}
</highlightSyntax>
+
</syntaxhighlight>
  
=Explanation=
+
'''CMakeLists.txt'''
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.
+
<syntaxhighlight lang="text">
 +
find_package(KDE4 REQUIRED)
 +
include_directories( ${KDE4_INCLUDES} )
 +
kde4_add_executable(printhelloworld main.cpp)
 +
target_link_libraries(printhelloworld ${KDE4_KDEUI_LIBS} ${QT_QTGUI_LIBRARY})
 +
</syntaxhighlight>
  
=How to compile=
+
=== Make and Run ===
==With KDE 4==
+
To build and run it, make sure you have your build environment set up correctly as described [[Getting_Started/Build|here]], then do:
Quite complicated:
+
  cmake .
  gcc print.cpp -o print -I/home/kde-devel/qt-unstable/include/Qt \
+
  make
  -I/home/kde-devel/qt-unstable/include/Qt-Core \
+
  ./printhelloworld
  -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==
+
== Next ==
Quite easy:
+
Next: [[Development/Tutorials/Printing_Print_Dialog|How to use the KDE print dialog in your programs]]
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=
+
[[Category:KDE4]]
* Point your konqueror to [http://developer.kde.org/documentation/library/cvs-api/kdelibs-apidocs/kdeprint/html/classKPrinter.html kde:kprinter]
+
[[Category:C++]]

Latest revision as of 19:11, 25 September 2011

Contents

[edit] The mission

Print Hello World on your printer. There will be no more dialog to confirm how many pages you want or the like.

[edit] The files

main.cpp

  1. #include <qpainter.h>
  2. #include <qprinter.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(
  15.                          // The program name used internally.
  16.                          "tutorial-printing",
  17.                          // The message catalog name
  18.                          // If null, program name is used instead.
  19.                          0,
  20.                          // A displayable program name string.
  21.                          ki18n("Printing Tutorial"),
  22.                          // The program version string.
  23.                          "1.0",
  24.                          // Short description of what the app does.
  25.                          ki18n("Displays a KMessageBox popup"),
  26.                          // The license this code is released under
  27.                          KAboutData::License_GPL,
  28.                          // Copyright Statement
  29.                          ki18n("(c) 2006-2011"),
  30.                          // Optional text shown in the About box.
  31.                          // Can contain any information desired.
  32.                          ki18n("Some text..."),
  33.                          // The program homepage string.
  34.                          "http://example.com/",
  35.                          // The bug report email address
  36.                          "submit@bugs.kde.org");
  37.     KCmdLineArgs::init( argc, argv, &aboutData );
  38.     KApplication app;
  39.  
  40.     QPrinter printer;
  41.     printer.setFullPage(true);
  42.     QPainter painter;
  43.     painter.begin(&printer);
  44.     painter.drawText(100,100,"Hello World");
  45.     painter.end(); 
  46.     // this makes the print job start
  47. }

CMakeLists.txt

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

[edit] Make and Run

To build and run it, make sure you have your build environment set up correctly as described here, then do:

cmake .
make
./printhelloworld

[edit] Next

Next: How to use the KDE print dialog in your programs


This page was last modified on 25 September 2011, at 19:11. This page has been accessed 13,080 times. Content is available under Creative Commons License SA 3.0 as well as the GNU Free Documentation License 1.2.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V.Legal