summaryrefslogtreecommitdiffstats
path: root/doc
diff options
context:
space:
mode:
authorGeir Vattekar <geir.vattekar@nokia.com>2010-09-20 08:38:09 (GMT)
committerGeir Vattekar <geir.vattekar@nokia.com>2010-09-20 08:38:09 (GMT)
commitf3a9990f6c46d596b9a53e0cca14c66d58b1dbe3 (patch)
treeec01bd645e664d241d643196fb930526c8e39e77 /doc
parentf412a9cb0bd1438d1518cf6a8eda3fda5fbf64f5 (diff)
downloadQt-f3a9990f6c46d596b9a53e0cca14c66d58b1dbe3.zip
Qt-f3a9990f6c46d596b9a53e0cca14c66d58b1dbe3.tar.gz
Qt-f3a9990f6c46d596b9a53e0cca14c66d58b1dbe3.tar.bz2
Doc: Added info on QWidget::render to printing docs
Task-number: QTBUG-2210 Reviewed-by: David Boddie
Diffstat (limited to 'doc')
-rw-r--r--doc/src/painting-and-printing/printing.qdoc11
-rw-r--r--doc/src/snippets/widgetprinting.cpp54
2 files changed, 65 insertions, 0 deletions
diff --git a/doc/src/painting-and-printing/printing.qdoc b/doc/src/painting-and-printing/printing.qdoc
index 62c8192..97cd92f 100644
--- a/doc/src/painting-and-printing/printing.qdoc
+++ b/doc/src/painting-and-printing/printing.qdoc
@@ -136,6 +136,17 @@
used is constructed using the form of the constructor that accepts a
QPaintDevice argument.
+ \section1 Printing Widgets
+
+ To print a widget, you can use the QWidget::render() function. As mentioned,
+ the printer's resolution is usually higher than the screen resolution, so you
+ will have to scale the painter. You may also want to position the widget on the
+ page. The following code sample shows how this may look.
+
+ \snippet doc/src/snippets/widgetprinting.cpp 0
+
+ This will center the widget on the page and scale it so that it fits the page.
+
\section1 Printing from Complex Widgets
Certain widgets, such as QTextEdit and QGraphicsView, display rich content
diff --git a/doc/src/snippets/widgetprinting.cpp b/doc/src/snippets/widgetprinting.cpp
new file mode 100644
index 0000000..b3d5b7c
--- /dev/null
+++ b/doc/src/snippets/widgetprinting.cpp
@@ -0,0 +1,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"
+