Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
widgetprinting.cpp
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
3
4#include <QtWidgets>
5#include <QtPrintSupport/qtprintsupportglobal.h>
6#if QT_CONFIG(printdialog)
7#include <QPrinter>
8#include <QPrintDialog>
9#endif
10
11class Window : public QWidget
12{
14
15public:
17 {
18 myWidget = new QPushButton("Print Me");
19 connect(myWidget, &QPushButton::clicked, this, &Window::print);
20 myWidget2 = new QPushButton("Print Document");
21 connect(myWidget2, &QPushButton::clicked, this, &Window::printFile);
22 editor = new QTextEdit(this);
23
25 layout->addWidget(myWidget);
26 layout->addWidget(myWidget2);
27 layout->addWidget(editor);
29 }
30
31private slots:
32 void print()
33 {
34 #if defined(QT_PRINTSUPPORT_LIB) && QT_CONFIG(printer)
36
37 printer.setOutputFileName("test.pdf");
38
41 painter.begin(&printer);
42 const auto pageLayout = printer.pageLayout();
43 const auto pageRect = pageLayout.paintRectPixels(printer.resolution());
44 const auto paperRect = pageLayout.fullRectPixels(printer.resolution());
45 double xscale = pageRect.width() / double(myWidget->width());
46 double yscale = pageRect.height() / double(myWidget->height());
47 double scale = qMin(xscale, yscale);
48 painter.translate(pageRect.x() + paperRect.width() / 2.,
49 pageRect.y() + paperRect.height() / 2.);
51 painter.translate(-myWidget->width() / 2., -myWidget->height() / 2.);
52
53 myWidget->render(&painter);
55 #endif
56 }
57
58 void printFile()
59 {
60 #if defined(QT_PRINTSUPPORT_LIB) && QT_CONFIG(printdialog)
62 QPrinter printer;
63
64 QPrintDialog dialog(&printer, this);
65 dialog.setWindowTitle(tr("Print Document"));
66 if (editor->textCursor().hasSelection())
69 return;
71 editor->print(&printer);
72 #endif
73 }
74
75private:
76 QPushButton *myWidget;
77 QPushButton *myWidget2;
78 QTextEdit *editor;
79};
80
81#include "main.moc"
82
83int main(int argv, char **args)
84{
85 QApplication app(argv, args);
86
88 window.show();
89
90 return app.exec();
91}
void clicked(bool checked=false)
This signal is emitted when the button is activated (i.e., pressed down then released while the mouse...
The QApplication class manages the GUI application's control flow and main settings.
static int exec()
Enters the main event loop and waits until exit() is called, then returns the value that was set to e...
virtual int exec()
Shows the dialog as a \l{QDialog::Modal Dialogs}{modal dialog}, blocking until the user closes it.
Definition qdialog.cpp:543
@ Accepted
Definition qdialog.h:30
void addWidget(QWidget *w)
Adds widget w to this layout in a manner specific to the layout.
Definition qlayout.cpp:186
static QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *member, Qt::ConnectionType=Qt::AutoConnection)
\threadsafe
Definition qobject.cpp:2960
The QPainter class performs low-level painting on widgets and other paint devices.
Definition qpainter.h:46
bool begin(QPaintDevice *)
Begins painting the paint device and returns true if successful; otherwise returns false.
void scale(qreal sx, qreal sy)
Scales the coordinate system by ({sx}, {sy}).
void translate(const QPointF &offset)
Translates the coordinate system by the given offset; i.e.
The QPrintDialog class provides a dialog for specifying the printer's configuration.
\reentrant
Definition qprinter.h:28
@ HighResolution
Definition qprinter.h:31
The QPushButton widget provides a command button.
Definition qpushbutton.h:20
bool hasSelection() const
Returns true if the cursor contains a selection; otherwise returns false.
The QTextEdit class provides a widget that is used to edit and display both plain and rich text.
Definition qtextedit.h:27
QTextCursor textCursor() const
Returns a copy of the QTextCursor that represents the currently visible cursor.
void print(QPagedPaintDevice *printer) const
The QVBoxLayout class lines up widgets vertically.
Definition qboxlayout.h:91
The QWidget class is the base class of all user interface objects.
Definition qwidget.h:99
void setLayout(QLayout *)
Sets the layout manager for this widget to layout.
QLayout * layout() const
Returns the layout manager that is installed on this widget, or \nullptr if no layout manager is inst...
int width
the width of the widget excluding any window frame
Definition qwidget.h:114
int height
the height of the widget excluding any window frame
Definition qwidget.h:115
void setWindowTitle(const QString &)
Definition qwidget.cpp:6105
void render(QPaintDevice *target, const QPoint &targetOffset=QPoint(), const QRegion &sourceRegion=QRegion(), RenderFlags renderFlags=RenderFlags(DrawWindowBackground|DrawChildren))
Definition qwidget.cpp:5092
[Window class with invokable method]
Definition window.h:11
QPushButton
[1]
int main()
[0]
constexpr const T & qMin(const T &a, const T &b)
Definition qminmax.h:40
GLenum GLenum GLuint GLint GLint GLint yscale
GLenum GLenum GLuint GLint GLint xscale
GLenum GLenum GLenum GLenum GLenum scale
#define tr(X)
#define Q_OBJECT
#define slots
QFileDialog dialog(this)
[1]
QApplication app(argc, argv)
[0]
QPainter painter(this)
[7]
aWidget window() -> setWindowTitle("New Window Title")
[2]
QJSValueList args