Development/Tutorials/Printing Print Dialog: Difference between revisions

From KDE TechBase
m (Text replace - "<code cppqt n>" to "<syntaxhighlight lang="cpp-qt" line>")
(Mark for updating)
 
(7 intermediate revisions by 2 users not shown)
Line 1: Line 1:
{{Review|Port to KF5}}
== The mission ==
== The mission ==


Line 5: Line 7:
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 28:
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
                        "[email protected]");
     KCmdLineArgs::init( argc, argv, &aboutData );
     KCmdLineArgs::init( argc, argv, &aboutData );
     KApplication app;
     KApplication app;
Line 37: Line 62:
     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 83:
}
}


void KHelloWorld::slotFilePrintPreview() {
void KHelloWorld::slotFilePrintPreview()  
{
     QPrinter printer;
     QPrinter printer;
     KPrintPreview printPreview(&printer);
     KPrintPreview printPreview(&printer);
Line 62: Line 90:
     printPreview.exec();
     printPreview.exec();
}
}
</code>
</syntaxhighlight>
 
== How to compile ==


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


<code>
<syntaxhighlight lang="text">
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})
</code>
</syntaxhighlight>


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


Then do:
Then do:

Latest revision as of 12:38, 31 May 2019

Warning
This page needs a review and probably holds information that needs to be fixed.

Parts to be reviewed:

Port to KF5

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 <QPrintDialog>
#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(
                         // 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
                         "[email protected]");
    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 main.cpp)
target_link_libraries(printhelloworld ${KDE4_KDECORE_LIBS} ${KDE4_KUTILS_LIBS} ${QT_QTGUI_LIBRARY})

Make and Run

Then do:

cmake .
make
./printhelloworld