Development/Tutorials/Printing Print Dialog: Difference between revisions

From KDE TechBase
No edit summary
No edit summary
Line 7: Line 7:
== The files ==
== The files ==


'''main.cpp'''
=== main.cpp ===
<syntaxhighlight lang="cpp-qt" line>
<syntaxhighlight lang="cpp-qt" line>
#include <QtGui/QPainter>
#include <QtGui/QPainter>
Line 69: Line 69:
</syntaxhighlight>
</syntaxhighlight>


'''CMakeLists.txt'''
=== CMakeLists.txt ===


<syntaxhighlight lang="text">
<syntaxhighlight lang="text">
Line 78: Line 78:
</syntaxhighlight>
</syntaxhighlight>


=== Make and Run ===
== Make and Run ==


Then do:
Then do:

Revision as of 19:03, 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

#include <QtGui/QPainter>
#include <QtGui/QPrinter>

#include <KdePrintDialog>
#include <KPrintPreview>

#include <KApplication>
#include <KAboutData>
#include <KAction>
#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;
    printer.setFullPage(true);
    QPrintDialog *printDialog = KdePrint::createPrintDialog(&printer, QList<QWidget*>() << &yourOptionsWidget, this);
    printDialog.setWindowTitle(i18n("Print Hello World"));
    if (printDialog.exec()) 
    {
        QPainter painter;
        painter.begin(&printer);
        painter.drawText(100,100,"Hello World");
        if (optionsWidget.printMoreText()) 
        {
        }
        painter.end(); 
        // 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
    printPreview.exec();
}

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