Development/Tutorials/Printing Hello World: Difference between revisions

From KDE TechBase
No edit summary
(3 intermediate revisions by the same user not shown)
Line 3: Line 3:
Print '''Hello World''' on your printer. There will be no more dialog to confirm how many pages you want or the like.
Print '''Hello World''' on your printer. There will be no more dialog to confirm how many pages you want or the like.


== The code ==
== The files ==
'''main.cpp'''
<syntaxhighlight lang="cpp-qt" line>
<syntaxhighlight lang="cpp-qt" line>
#include <qpainter.h>
#include <qpainter.h>
Line 54: Line 55:
</syntaxhighlight>
</syntaxhighlight>


== How to compile ==
'''CMakeLists.txt'''
 
=== CMakeLists.txt ===
 
<syntaxhighlight lang="text">
<syntaxhighlight lang="text">
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 main.cpp)
target_link_libraries(printhelloworld ${KDE4_KDEUI_LIBS} ${QT_QTGUI_LIBRARY})
target_link_libraries(printhelloworld ${KDE4_KDEUI_LIBS} ${QT_QTGUI_LIBRARY})
</syntaxhighlight>
</syntaxhighlight>


=== Make and Run ===
=== Make and Run ===
 
To build and run it, make sure you have your build environment set up correctly as described [[Getting_Started/Build|here]], then do:
Then do:
  cmake .
  cmake .
  make
  make
  ./printhelloworld
  ./printhelloworld
== Next ==
Next: [[Development/Tutorials/Printing_Print_Dialog|How to use the KDE print dialog in your programs]]


[[Category:KDE4]]
[[Category:KDE4]]
[[Category:C++]]
[[Category:C++]]

Revision as of 18:11, 25 September 2011

The mission

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

The files

main.cpp

#include <qpainter.h>
#include <qprinter.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(
                         // The program name used internally.
                         "tutorial-printing",
                         // The message catalog name
                         // 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
                         "[email protected]");
    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
}

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})

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

Next

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