QtBase  v6.3.1
widgetprinting.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2016 The Qt Company Ltd.
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of the documentation of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:BSD$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see https://www.qt.io/terms-conditions. For further
15 ** information use the contact form at https://www.qt.io/contact-us.
16 **
17 ** BSD License Usage
18 ** Alternatively, you may use this file under the terms of the BSD license
19 ** as follows:
20 **
21 ** "Redistribution and use in source and binary forms, with or without
22 ** modification, are permitted provided that the following conditions are
23 ** met:
24 ** * Redistributions of source code must retain the above copyright
25 ** notice, this list of conditions and the following disclaimer.
26 ** * Redistributions in binary form must reproduce the above copyright
27 ** notice, this list of conditions and the following disclaimer in
28 ** the documentation and/or other materials provided with the
29 ** distribution.
30 ** * Neither the name of The Qt Company Ltd nor the names of its
31 ** contributors may be used to endorse or promote products derived
32 ** from this software without specific prior written permission.
33 **
34 **
35 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
36 ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
37 ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
38 ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
39 ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40 ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
41 ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
42 ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
43 ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
44 ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
45 ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
46 **
47 ** $QT_END_LICENSE$
48 **
49 ****************************************************************************/
50 
51 #include <QtWidgets>
52 #include <QtPrintSupport/qtprintsupportglobal.h>
53 #if QT_CONFIG(printdialog)
54 #include <QPrinter>
55 #include <QPrintDialog>
56 #endif
57 
58 class Window : public QWidget
59 {
60  Q_OBJECT
61 
62 public:
64  {
65  myWidget = new QPushButton("Print Me");
66  connect(myWidget, &QPushButton::clicked, this, &Window::print);
67  myWidget2 = new QPushButton("Print Document");
68  connect(myWidget2, &QPushButton::clicked, this, &Window::printFile);
69  editor = new QTextEdit(this);
70 
72  layout->addWidget(myWidget);
73  layout->addWidget(myWidget2);
74  layout->addWidget(editor);
76  }
77 
78 private slots:
79  void print()
80  {
81  #if defined(QT_PRINTSUPPORT_LIB) && QT_CONFIG(printer)
83 
84  printer.setOutputFileName("test.pdf");
85 
88  painter.begin(&printer);
89  double xscale = printer.pageRect().width() / double(myWidget->width());
90  double yscale = printer.pageRect().height() / double(myWidget->height());
91  double scale = qMin(xscale, yscale);
92  painter.translate(printer.paperRect().x() + printer.pageRect().width()/2,
93  printer.paperRect().y() + printer.pageRect().height()/2);
95  painter.translate(-width()/2, -height()/2);
96 
97  myWidget->render(&painter);
99  #endif
100  }
101 
102  void printFile()
103  {
104  #if defined(QT_PRINTSUPPORT_LIB) && QT_CONFIG(printdialog)
106  QPrinter printer;
107 
108  QPrintDialog dialog(&printer, this);
109  dialog.setWindowTitle(tr("Print Document"));
110  if (editor->textCursor().hasSelection())
112  if (dialog.exec() != QDialog::Accepted) {
113  return;
114  }
116  editor->print(&printer);
117  #endif
118  }
119 
120 private:
121  QPushButton *myWidget;
122  QPushButton *myWidget2;
123  QTextEdit *editor;
124 };
125 
126 #include "main.moc"
127 
128 int main(int argv, char **args)
129 {
130  QApplication app(argv, args);
131 
132  Window window;
133  window.show();
134 
135  return app.exec();
136 }
void clicked(bool checked=false)
The QApplication class manages the GUI application's control flow and main settings.
Definition: qapplication.h:68
static int exec()
virtual int exec()
Definition: qdialog.cpp:595
@ Accepted
Definition: qdialog.h:66
friend class QPushButton
[1]
Definition: qdialog.h:57
void setLayout(QGraphicsLayout *layout)
QGraphicsLayout * layout
The layout of the widget.
static QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *member, Qt::ConnectionType=Qt::AutoConnection)
Definition: qobject.cpp:2772
The QPainter class performs low-level painting on widgets and other paint devices.
Definition: qpainter.h:82
bool begin(QPaintDevice *)
Definition: qpainter.cpp:1709
void scale(qreal sx, qreal sy)
Definition: qpainter.cpp:2922
void translate(const QPointF &offset)
Definition: qpainter.cpp:2993
The QPrintDialog class provides a dialog for specifying the printer's configuration.
Definition: qprintdialog.h:56
The QPrinter class is a paint device that paints on a printer.
Definition: qprinter.h:64
@ HighResolution
Definition: qprinter.h:67
The QPushButton widget provides a command button.
Definition: qpushbutton.h:56
bool hasSelection() const
The QTextEdit class provides a widget that is used to edit and display both plain and rich text.
Definition: qtextedit.h:63
QTextCursor textCursor() const
Definition: qtextedit.cpp:871
void print(QPagedPaintDevice *printer) const
Definition: qtextedit.cpp:2481
The QVBoxLayout class lines up widgets vertically.
Definition: qboxlayout.h:127
The QWidget class is the base class of all user interface objects.
Definition: qwidget.h:133
int width
the width of the widget excluding any window frame
Definition: qwidget.h:148
int height
the height of the widget excluding any window frame
Definition: qwidget.h:149
void setWindowTitle(const QString &)
Definition: qwidget.cpp:6119
void render(QPaintDevice *target, const QPoint &targetOffset=QPoint(), const QRegion &sourceRegion=QRegion(), RenderFlags renderFlags=RenderFlags(DrawWindowBackground|DrawChildren))
int width
the width of the window's geometry
Definition: qwindow.h:118
int height
the height of the window's geometry
Definition: qwindow.h:119
[Window class definition]
Definition: window.h:64
GLenum GLenum GLuint GLint GLint GLint yscale
GLenum GLenum GLuint GLint GLint xscale
GLenum GLenum GLenum GLenum GLenum scale
Definition: qopenglext.h:10817
#define tr(X)
#define Q_OBJECT
Definition: qtmetamacros.h:158
#define slots
Definition: qtmetamacros.h:76
QFileDialog dialog(this)
[1]
QApplication app(argc, argv)
[0]
QPainter painter(this)
[7]
aWidget window() -> setWindowTitle("New Window Title")
[2]
int main(int argv, char **args)
[1]