Qt 5.10 Book Examples
Printer.cpp
Go to the documentation of this file.
1 // ======================================================================
2 // Printer.cpp
3 // ======================================================================
4 // This file is a part of the book
5 // "Qt 5.10 Professional programming with C++"
6 // http://qt-book.com
7 // ======================================================================
8 // Copyright (c) 2017 by Max Schlee
9 // ======================================================================
10 
11 #include <QtWidgets>
12 #include <QtPrintSupport>
13 #include "Printer.h"
14 
15 // ----------------------------------------------------------------------
16 Printer::Printer(QWidget* pwgt/*=0*/) : QWidget(pwgt)
17 {
18  m_pprinter = new QPrinter;
19 }
20 
21 // ----------------------------------------------------------------------
22 /*virtual*/Printer::~Printer()
23 {
24  delete m_pprinter;
25 }
26 
27 // ----------------------------------------------------------------------
28 /*virtual*/ void Printer::paintEvent(QPaintEvent*)
29 {
30  draw(this);
31 }
32 
33 // ----------------------------------------------------------------------
35 {
36  QPrintDialog dlg(m_pprinter, this);
37 
38  dlg.setMinMax(1, 1);
39  if (dlg.exec() == QDialog::Accepted) {
40  draw(m_pprinter);
41  }
42 }
43 
44 // ----------------------------------------------------------------------
45 void Printer::draw(QPaintDevice* ppd)
46 {
47  QPainter painter(ppd);
48  QRect r(painter.viewport());
49 
50  painter.setBrush(Qt::white);
51  painter.drawRect(r);
52  painter.drawLine(0, 0, r.width(), r.height());
53  painter.drawLine(r.width(), 0, 0, r.height());
54 
55  painter.setBrush(Qt::NoBrush);
56  painter.setPen(QPen(Qt::red, 3, Qt::DashLine));
57  painter.drawEllipse(r);
58 
59  painter.setPen(Qt::blue);
60  painter.setFont(QFont("Times", 20, QFont::Normal));
61  painter.drawText(r, Qt::AlignCenter, "Printer Test");
62 }
void draw(QPaintDevice *ppd)
Definition: Printer.cpp:45
void slotPrint()
Definition: Printer.cpp:34
Printer(QWidget *pwgt=0)
Definition: Printer.cpp:16
virtual void paintEvent(QPaintEvent *pe)
Definition: Printer.cpp:28
virtual ~Printer()
Definition: Printer.cpp:22