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

Jump to: navigation, search
m (Text replace - "</code>" to "</syntaxhighlight>")
(main.cpp)
 
(4 intermediate revisions by one user not shown)
Line 5: Line 5:
 
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.
 
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.
  
== The code ==
+
== The files ==
  
 +
=== main.cpp ===
 
<syntaxhighlight lang="cpp-qt" line>
 
<syntaxhighlight lang="cpp-qt" line>
 
#include <QtGui/QPainter>
 
#include <QtGui/QPainter>
 
#include <QtGui/QPrinter>
 
#include <QtGui/QPrinter>
  
#include <KdePrintDialog>
+
#include <QPrintDialog>
 
#include <KPrintPreview>
 
#include <KPrintPreview>
  
 
#include <KApplication>
 
#include <KApplication>
 
#include <KAboutData>
 
#include <KAboutData>
 +
#include <KAction>
 
#include <KCmdLineArgs>
 
#include <KCmdLineArgs>
  
Line 24: 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;
Line 37: Line 60:
 
     QPrintDialog *printDialog = KdePrint::createPrintDialog(&printer, QList<QWidget*>() << &yourOptionsWidget, this);
 
     QPrintDialog *printDialog = KdePrint::createPrintDialog(&printer, QList<QWidget*>() << &yourOptionsWidget, this);
 
     printDialog.setWindowTitle(i18n("Print Hello World"));
 
     printDialog.setWindowTitle(i18n("Print Hello World"));
     if (printDialog.exec()) {
+
     if (printDialog.exec())  
 +
    {
 
         QPainter painter;
 
         QPainter painter;
 
         painter.begin(&printer);
 
         painter.begin(&printer);
 
         painter.drawText(100,100,"Hello World");
 
         painter.drawText(100,100,"Hello World");
         if (optionsWidget.printMoreText()) {
+
         if (optionsWidget.printMoreText())  
 +
        {
 
         }
 
         }
 
         painter.end();  
 
         painter.end();  
Line 56: Line 81:
 
}
 
}
  
void KHelloWorld::slotFilePrintPreview() {
+
void KHelloWorld::slotFilePrintPreview()  
 +
{
 
     QPrinter printer;
 
     QPrinter printer;
 
     KPrintPreview printPreview(&printer);
 
     KPrintPreview printPreview(&printer);
Line 63: Line 89:
 
}
 
}
 
</syntaxhighlight>
 
</syntaxhighlight>
 
== How to compile ==
 
  
 
=== CMakeLists.txt ===
 
=== CMakeLists.txt ===
Line 71: Line 95:
 
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_KDECORE_LIBS} ${KDE4_KUTILS_LIBS} ${QT_QTGUI_LIBRARY})
+
target_link_libraries(printhelloworld ${KDE4_KDECORE_LIBS} ${KDE4_KUTILS_LIBS} ${QT_QTGUI_LIBRARY})
 
</syntaxhighlight>
 
</syntaxhighlight>
  
=== Make and Run ===
+
== Make and Run ==
  
 
Then do:
 
Then do:

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,366 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