Development/Tutorials/Printing Hello World: Difference between revisions

From KDE TechBase
(Undo revision 38831 by Midnighter (Talk))
(Mark for updating)
 
(11 intermediate revisions by 3 users not shown)
Line 1: Line 1:
{{note|This tutorial seems not to be up to date with current KDE4 development.}}
{{Review|Port to KF5}}


=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 ==
<code cppqt n>
'''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 22: Line 21:
int main(int argc, char *argv[])
int main(int argc, char *argv[])
{
{
  KAboutData aboutData( "test", "test", "1.0", "test",
    KAboutData aboutData(
      KAboutData::License_GPL, "(c) 2006" );
                        // The program name used internally.
  KCmdLineArgs::init( argc, argv, &aboutData );
                        "tutorial-printing",
  KApplication app;
                        // 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;


  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>
</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">
 
=How to compile=
==With KDE 4 and CMake==
===CMakeLists.txt===
<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 main.cpp)
target_link_libraries(printhelloworld ${KDE4_KDEPRINT_LIBS})
target_link_libraries(printhelloworld ${KDE4_KDEUI_LIBS} ${QT_QTGUI_LIBRARY})
</code>
</syntaxhighlight>
===Make and Run===
 
Then do:
=== 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:
  cmake .
  cmake .
  make
  make
  ./printhelloworld
  ./printhelloworld


==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=
* 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++]]

Latest revision as of 12:38, 31 May 2019

Warning
This page needs a review and probably holds information that needs to be fixed.

Parts to be reviewed:

Port to KF5

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