Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
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
11
class
Window
:
public
QWidget
12
{
13
Q_OBJECT
14
15
public
:
16
Window
()
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
24
QVBoxLayout
*
layout
=
new
QVBoxLayout
;
25
layout
->
addWidget
(
myWidget
);
26
layout
->
addWidget
(
myWidget2
);
27
layout
->
addWidget
(
editor
);
28
setLayout
(
layout
);
29
}
30
31
private
slots
:
32
void
print
()
33
{
34
#
if
defined
(
QT_PRINTSUPPORT_LIB
)
&&
QT_CONFIG
(
printer
)
35
QPrinter
printer
(
QPrinter
::
HighResolution
);
36
37
printer
.
setOutputFileName
(
"test.pdf"
);
38
39
//! [0]
40
QPainter
painter
;
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.);
50
painter
.
scale
(
scale
,
scale
);
51
painter
.
translate
(-
myWidget
->
width
() / 2., -
myWidget
->
height
() / 2.);
52
53
myWidget
->
render
(&
painter
);
54
//! [0]
55
#
endif
56
}
57
58
void
printFile
()
59
{
60
#
if
defined
(
QT_PRINTSUPPORT_LIB
)
&&
QT_CONFIG
(
printdialog
)
61
//! [1]
62
QPrinter
printer
;
63
64
QPrintDialog
dialog
(&
printer
,
this
);
65
dialog
.
setWindowTitle
(
tr
(
"Print Document"
));
66
if
(
editor
->
textCursor
().
hasSelection
())
67
dialog
.
addEnabledOption
(
QAbstractPrintDialog
::
PrintSelection
);
68
if
(
dialog
.
exec
() !=
QDialog
::
Accepted
)
69
return
;
70
//! [1]
71
editor
->
print
(&
printer
);
72
#
endif
73
}
74
75
private
:
76
QPushButton
*
myWidget
;
77
QPushButton *myWidget2;
78
QTextEdit *editor;
79
};
80
81
#
include
"main.moc"
82
83
int
main
(
int
argv,
char
**args)
84
{
85
QApplication app(argv, args);
86
87
Window
window;
88
window.show();
89
90
return
app.exec();
91
}
Window
[Window class with invokable method]
Definition
window.h:11
main
int main(int argc, char *argv[])
[ctor_close]
Definition
src_corelib_animation_qpropertyanimation.cpp:100
qtbase
src
printsupport
doc
snippets
widgetprinting.cpp
Generated on
for Qt by
1.14.0