Development/Tutorials/Printing Print Dialog
< Development | Tutorials
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 <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 "[email protected]");
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 }
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})
Make and Run
Then do:
cmake . make ./printhelloworld
Content is available under Creative Commons License SA 4.0 unless otherwise noted.