Difference between revisions of "Development/Tutorials/Printing Print Dialog"

Jump to: navigation, search
(main.cpp)
 
Line 26: Line 26:
 
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.
 +
                        "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
 +
                        "submit@bugs.kde.org");
 
     KCmdLineArgs::init( argc, argv, &aboutData );
 
     KCmdLineArgs::init( argc, argv, &aboutData );
 
     KApplication app;
 
     KApplication app;

Latest revision as of 20:06, 25 September 2011

Contents

[edit] The mission

To display the Print Dialog and print a document.

KDE uses the Qt Printing infrastructure, but adds extra standard features to the Print Dialog, as well as allowing applications to add their own extensions.

[edit] The files

[edit] main.cpp

  1. #include <QtGui/QPainter>
  2. #include <QtGui/QPrinter>
  3.  
  4. #include <QPrintDialog>
  5. #include <KPrintPreview>
  6.  
  7. #include <KApplication>
  8. #include <KAboutData>
  9. #include <KAction>
  10. #include <KCmdLineArgs>
  11.  
  12. /*
  13. This prints Hello World on your printer
  14. */
  15.  
  16. int main(int argc, char *argv[])
  17. {
  18.     KAboutData aboutData(
  19.                          // The program name used internally.
  20.                          "tutorial-printing",
  21.                          // The message catalog name
  22.                          // If null, program name is used instead.
  23.                          0,
  24.                          // A displayable program name string.
  25.                          ki18n("Printing Tutorial"),
  26.                          // The program version string.
  27.                          "1.0",
  28.                          // Short description of what the app does.
  29.                          ki18n("Displays a KMessageBox popup"),
  30.                          // The license this code is released under
  31.                          KAboutData::License_GPL,
  32.                          // Copyright Statement
  33.                          ki18n("(c) 2006-2011"),
  34.                          // Optional text shown in the About box.
  35.                          // Can contain any information desired.
  36.                          ki18n("Some text..."),
  37.                          // The program homepage string.
  38.                          "http://example.com/",
  39.                          // The bug report email address
  40.                          "submit@bugs.kde.org");
  41.     KCmdLineArgs::init( argc, argv, &aboutData );
  42.     KApplication app;
  43.  
  44.     KAction* m_printPreview = KStandardAction::printPreview(app, SLOT(slotFilePrintPreview()), actionCollection());
  45.  
  46.     KHelloWorldPrintDialogWidget optionsWidget;
  47.  
  48.     QPrinter printer;
  49.     printer.setFullPage(true);
  50.     QPrintDialog *printDialog = KdePrint::createPrintDialog(&printer, QList<QWidget*>() << &yourOptionsWidget, this);
  51.     printDialog.setWindowTitle(i18n("Print Hello World"));
  52.     if (printDialog.exec()) 
  53.     {
  54.         QPainter painter;
  55.         painter.begin(&printer);
  56.         painter.drawText(100,100,"Hello World");
  57.         if (optionsWidget.printMoreText()) 
  58.         {
  59.         }
  60.         painter.end(); 
  61.         // this makes the print job start
  62.     }
  63. }
  64.  
  65. class KHelloWorldPrintDialogWidget : public QWidget
  66. {
  67.     bool printMoreText();
  68.     void setPrintMoreText(bool state);
  69.  
  70.     setWindowTitle(i18n("Your Options Tab Title"));
  71. }
  72.  
  73. void KHelloWorld::slotFilePrintPreview() 
  74. {
  75.     QPrinter printer;
  76.     KPrintPreview printPreview(&printer);
  77.     yourPrintDrawingRoutine(printer); // draws to the QPrinter
  78.     printPreview.exec();
  79. }

[edit] CMakeLists.txt

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

[edit] Make and Run

Then do:

cmake .
make
./printhelloworld

This page was last modified on 25 September 2011, at 20:06. This page has been accessed 3,364 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