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

Jump to: navigation, search
(if you do changes to the tutorials, please test them at least once)
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>
Line 16: Line 17:
 
#include <KApplication>
 
#include <KApplication>
 
#include <KAboutData>
 
#include <KAboutData>
 +
#include <KAction>
 
#include <KCmdLineArgs>
 
#include <KCmdLineArgs>
  
Line 37: Line 39:
 
     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 60:
 
}
 
}
  
void KHelloWorld::slotFilePrintPreview() {
+
void KHelloWorld::slotFilePrintPreview()  
 +
{
 
     QPrinter printer;
 
     QPrinter printer;
 
     KPrintPreview printPreview(&printer);
 
     KPrintPreview printPreview(&printer);
Line 64: Line 69:
 
</syntaxhighlight>
 
</syntaxhighlight>
  
== How to compile ==
+
'''CMakeLists.txt'''
 
+
=== CMakeLists.txt ===
+
  
 
<syntaxhighlight lang="text">
 
<syntaxhighlight lang="text">

Revision as of 20:02, 25 September 2011

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.

The files

main.cpp

  1. #include <QtGui/QPainter>
  2. #include <QtGui/QPrinter>
  3.  
  4. #include <KdePrintDialog>
  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( "test", "test", "1.0", "test",
  19.                           KAboutData::License_GPL, "(c) 2006" );
  20.     KCmdLineArgs::init( argc, argv, &aboutData );
  21.     KApplication app;
  22.  
  23.     KAction* m_printPreview = KStandardAction::printPreview(app, SLOT(slotFilePrintPreview()), actionCollection());
  24.  
  25.     KHelloWorldPrintDialogWidget optionsWidget;
  26.  
  27.     QPrinter printer;
  28.     printer.setFullPage(true);
  29.     QPrintDialog *printDialog = KdePrint::createPrintDialog(&printer, QList<QWidget*>() << &yourOptionsWidget, this);
  30.     printDialog.setWindowTitle(i18n("Print Hello World"));
  31.     if (printDialog.exec()) 
  32.     {
  33.         QPainter painter;
  34.         painter.begin(&printer);
  35.         painter.drawText(100,100,"Hello World");
  36.         if (optionsWidget.printMoreText()) 
  37.         {
  38.         }
  39.         painter.end(); 
  40.         // this makes the print job start
  41.     }
  42. }
  43.  
  44. class KHelloWorldPrintDialogWidget : public QWidget
  45. {
  46.     bool printMoreText();
  47.     void setPrintMoreText(bool state);
  48.  
  49.     setWindowTitle(i18n("Your Options Tab Title"));
  50. }
  51.  
  52. void KHelloWorld::slotFilePrintPreview() 
  53. {
  54.     QPrinter printer;
  55.     KPrintPreview printPreview(&printer);
  56.     yourPrintDrawingRoutine(printer); // draws to the QPrinter
  57.     printPreview.exec();
  58. }

CMakeLists.txt

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

Make and Run

Then do:

cmake .
make
./printhelloworld

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V.Legal