< Development‎ | Tutorials
Revision as of 20:45, 29 June 2011 by Neverendingo (talk | contribs) (Text replace - "<code>" to "<syntaxhighlight lang="text">")

Development/Tutorials/Printing Print Dialog

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 code

<syntaxhighlight lang="cpp-qt" line>

  1. include <QtGui/QPainter>
  2. include <QtGui/QPrinter>
  1. include <KdePrintDialog>
  2. include <KPrintPreview>
  1. include <KApplication>
  2. include <KAboutData>
  3. include <KCmdLineArgs>

/* This prints Hello World on your printer

  • /

int main(int argc, char *argv[]) {

   KAboutData aboutData( "test", "test", "1.0", "test",
                         KAboutData::License_GPL, "(c) 2006" );
   KCmdLineArgs::init( argc, argv, &aboutData );
   KApplication app;
   KAction* m_printPreview = KStandardAction::printPreview(app, SLOT(slotFilePrintPreview()), actionCollection());
   KHelloWorldPrintDialogWidget optionsWidget;
   QPrinter printer;
   QPrintDialog *printDialog = KdePrint::createPrintDialog(&printer, QList<QWidget*>() << &yourOptionsWidget, this);
   printDialog.setWindowTitle(i18n("Print Hello World"));
   if (printDialog.exec()) {
       QPainter painter;
       painter.drawText(100,100,"Hello World");
       if (optionsWidget.printMoreText()) {
       // this makes the print job start


class KHelloWorldPrintDialogWidget : public QWidget {

   bool printMoreText();
   void setPrintMoreText(bool state);
   setWindowTitle(i18n("Your Options Tab Title"));


void KHelloWorld::slotFilePrintPreview() {

   QPrinter printer;
   KPrintPreview printPreview(&printer);
   yourPrintDrawingRoutine(printer); // draws to the QPrinter


How to compile


<syntaxhighlight lang="text"> 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 .

This page was last edited on 31 May 2019, at 12:38. Content is available under Creative Commons License SA 4.0 unless otherwise noted.