summaryrefslogtreecommitdiffstats
path: root/doc/src/snippets/widgetprinting.cpp
blob: b3d5b7c9aa7aba4b09138de49d065461bcbff46a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54

#include <QtGui>

class Window : public QWidget
{
    Q_OBJECT

public:
    Window() {
        myWidget = new QPushButton("Print Me");
        connect(myWidget, SIGNAL(clicked()), this, SLOT(print()));

        QVBoxLayout *layout = new QVBoxLayout;
        layout->addWidget(myWidget);
        setLayout(layout);
    }

private slots:
    void print() {
        QPrinter printer(QPrinter::HighResolution);
        
        printer.setOutputFileName("test.pdf");

//! [0]
        QPainter painter;
        painter.begin(&printer);
        double xscale = printer.pageRect().width()/double(myWidget->width());
        double yscale = printer.pageRect().height()/double(myWidget->height());
        double scale = qMin(xscale, yscale);
        painter.translate(printer.paperRect().x() + printer.pageRect().width()/2,
                           printer.paperRect().y() + printer.pageRect().height()/2);
        painter.scale(scale, scale);
        painter.translate(-width()/2, -height()/2);

        myWidget->render(&painter);
//! [0]
    }

private:
    QPushButton *myWidget;
};

int main(int argv, char **args)
{
    QApplication app(argv, args);

    Window window;
    window.show();

    return app.exec();
}

#include "main.moc"