Part 12: Print and Print Preview

Part 12: Print and Print Preview


[<< Contents] [<< Prev] [Next >>]

Printing and print preview with Qt applications

To support printing and generating a print preview we only need to make enhancements to our MainWindow class. Our printed output will be a single page showing the simulated landscape as shown by our scene and some labels on the corners of the page. The hard work will be performed by the Qt classes QPrinter, QPrintPreviewDialog and QPrintDialog. We will also add two new File menu actions "Print preview..." and "Print...".

Enhancing the MainWindow

mainwindow.h

Add a forward declaration for the QPrinter class.

class QPrinter;

Add three public slot definitions, one for when the user selects the print preview action, one for when the user selects the print action, and one for drawing the printed page.

  void filePrintPreview();            // display print preview dialog
  void filePrint();                   // display print dialog
  void print( QPrinter* );            // draw print page

mainwindow.cpp

Include the header files for the QPrinter, QPrintPreviewDialog, and QPrintDialog functionality.

#include <QPrinter>
#include <QPrintPreviewDialog>
#include <QPrintDialog>

In the constructor below adding the open and save actions to the File menu, we add a separator and the "Print preview..." and "Print..." actions.

  fileMenu->addSeparator();
  QAction* previewAction = fileMenu->addAction( "Print pre&view...", this, SLOT(filePrintPreview()) );
  QAction* printAction   = fileMenu->addAction( "&Print...",         this, SLOT(filePrint()) );

Still in the constructor we set a keyboard shortcut for the print action.

  printAction->setShortcut( QKeySequence::Print );

Now we can add the code for the print preview public slot. In this we create a QPrinter object used for painting on a printer and the print preview dialog. We connect the print preview dialog to our print slot and pass control to the dialog.

/********************************* filePrintPreview **********************************/

void  MainWindow::filePrintPreview()
{
  // display print preview dialog
  QPrinter             printer( QPrinter::HighResolution );
  QPrintPreviewDialog  preview( &printer, this );
  connect( &preview, SIGNAL(paintRequested(QPrinter*)), SLOT(print(QPrinter*)) );
  preview.exec();
}

Here we add the code for the print dialog pubic slot. In this we create a QPrinter object and the print dialog. If the user accepts the dialog then we print.

/************************************ filePrint **************************************/

void  MainWindow::filePrint()
{
  // display print dialog and if accepted print
  QPrinter       printer( QPrinter::HighResolution );
  QPrintDialog   dialog( &printer, this );
  if ( dialog.exec() == QDialog::Accepted ) print( &printer );
}

Here we add the code for painting onto a printer as used by the print preview dialog and the print dialog. We create a painter based on the printer and set up a page rectangle. We then set a font size appropriate to the page size and draw labels in three of the four corners. Finally we get the scene to draw or simulation landscape in the middle of the page.

/*************************************** print ***************************************/

void  MainWindow::print( QPrinter* printer )
{
  // create painter for drawing print page
  QPainter painter( printer );
  int      w = printer->pageRect().width();
  int      h = printer->pageRect().height();
  QRect    page( 0, 0, w, h );

  // create a font appropriate to page size
  QFont    font = painter.font();
  font.setPixelSize( (w+h) / 100 );
  painter.setFont( font );

  // draw labels in corners of page
  painter.drawText( page, Qt::AlignTop    | Qt::AlignLeft, "QSimulate" );
  painter.drawText( page, Qt::AlignBottom | Qt::AlignLeft, QString(getenv("USER")) );
  painter.drawText( page, Qt::AlignBottom | Qt::AlignRight,
                    QDateTime::currentDateTime().toString( Qt::DefaultLocaleShortDate ) );

  // draw simulated landscape
  page.adjust( w/20, h/20, -w/20, -h/20 );
  m_scene->render( &painter, page );
}

Compile and run

The new code will automatically re-compile when you attempt to run the application. Play with the application and investigate the new functionality. Qt's printing system also enables PostScript and PDF files to be generated.


[<< Contents] [<< Prev] [Next >>]


Last updated 29-Jun-2008