Sophie

Sophie

distrib > Mageia > 6 > armv5tl > media > core-updates > by-pkgid > 768f7d9f703884aa2562bf0a651086df > files > 2193

qtbase5-doc-5.9.4-1.1.mga6.noarch.rpm

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html>
<html lang="en">
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<!-- qtprintsupport-index.qdoc -->
  <title>Qt Print Support 5.9</title>
  <link rel="stylesheet" type="text/css" href="style/offline-simple.css" />
  <script type="text/javascript">
    document.getElementsByTagName("link").item(0).setAttribute("href", "style/offline.css");
    // loading style sheet breaks anchors that were jumped to before
    // so force jumping to anchor again
    setTimeout(function() {
        var anchor = location.hash;
        // need to jump to different anchor first (e.g. none)
        location.hash = "#";
        setTimeout(function() {
            location.hash = anchor;
        }, 0);
    }, 0);
  </script>
</head>
<body>
<div class="header" id="qtdocheader">
  <div class="main">
    <div class="main-rounded">
      <div class="navigationbar">
        <table><tr>
<td >Qt 5.9</td><td >Qt Print Support</td></tr></table><table class="buildversion"><tr>
<td id="buildversion" width="100%" align="right">Qt 5.9.4 Reference Documentation</td>
        </tr></table>
      </div>
    </div>
<div class="content">
<div class="line">
<div class="content mainContent">
<div class="sidebar">
<div class="toc">
<h3><a name="toc">Contents</a></h3>
<ul>
<li class="level1"><a href="#classes-supporting-printing">Classes Supporting Printing</a></li>
<li class="level1"><a href="#paint-devices-and-printing">Paint Devices and Printing</a></li>
<li class="level2"><a href="#creating-a-qprinter">Creating a QPrinter</a></li>
<li class="level2"><a href="#painting-onto-a-page">Painting onto a Page</a></li>
<li class="level2"><a href="#coordinate-systems">Coordinate Systems</a></li>
<li class="level1"><a href="#printing-widgets">Printing Widgets</a></li>
<li class="level1"><a href="#printing-from-complex-widgets">Printing from Complex Widgets</a></li>
</ul>
</div>
<div class="sidebar-content" id="sidebar-content"></div></div>
<h1 class="title">Qt Print Support</h1>
<span class="subtitle"></span>
<!-- $$$qtprintsupport-index.html-description -->
<div class="descr"> <a name="details"></a>
<p>Qt provides extensive cross-platform support for printing. Using the printing systems on each platform, Qt applications can print to attached printers and across networks to remote printers. Qt's printing system also supports PDF file generation, providing the foundation for basic report generation facilities.</p>
<a name="classes-supporting-printing"></a>
<h2 id="classes-supporting-printing">Classes Supporting Printing</h2>
<p>The following classes support the selecting and setting up of printers and printing output.</p>
<div class="table"><table class="annotated">
<tr class="odd topAlign"><td class="tblName"><p><a href="qabstractprintdialog.html">QAbstractPrintDialog</a></p></td><td class="tblDescr"><p>Base implementation for print dialogs used to configure printers</p></td></tr>
<tr class="even topAlign"><td class="tblName"><p><a href="qpagesetupdialog.html">QPageSetupDialog</a></p></td><td class="tblDescr"><p>Configuration dialog for the page-related options on a printer</p></td></tr>
<tr class="odd topAlign"><td class="tblName"><p><a href="qprintdialog.html">QPrintDialog</a></p></td><td class="tblDescr"><p>Dialog for specifying the printer's configuration</p></td></tr>
<tr class="even topAlign"><td class="tblName"><p><a href="qprintpreviewdialog.html">QPrintPreviewDialog</a></p></td><td class="tblDescr"><p>Dialog for previewing and configuring page layouts for printer output</p></td></tr>
<tr class="odd topAlign"><td class="tblName"><p><a href="qprintengine.html">QPrintEngine</a></p></td><td class="tblDescr"><p>Defines an interface for how QPrinter interacts with a given printing subsystem</p></td></tr>
<tr class="even topAlign"><td class="tblName"><p><a href="qprinter.html">QPrinter</a></p></td><td class="tblDescr"><p>Paint device that paints on a printer</p></td></tr>
<tr class="odd topAlign"><td class="tblName"><p><a href="qprinterinfo.html">QPrinterInfo</a></p></td><td class="tblDescr"><p>Gives access to information about existing printers</p></td></tr>
<tr class="even topAlign"><td class="tblName"><p><a href="qprintpreviewwidget.html">QPrintPreviewWidget</a></p></td><td class="tblDescr"><p>Widget for previewing page layouts for printer output</p></td></tr>
</table></div>
<a name="paint-devices-and-printing"></a>
<h2 id="paint-devices-and-printing">Paint Devices and Printing</h2>
<p>In Qt, printers are represented by <a href="qprinter.html">QPrinter</a>, a paint device that provides functionality specific to printing, such as support for multiple pages and double-sided output. As a result, printing involves using a <a href="../qtgui/qpainter.html">QPainter</a> to paint onto a series of pages in the same way that you would paint onto a custom widget or image.</p>
<a name="creating-a-qprinter"></a>
<h3 >Creating a QPrinter</h3>
<p>Although <a href="qprinter.html">QPrinter</a> objects can be constructed and set up without requiring user input, printing is often performed as a result of a request by the user; for example, when the user selects the <b>File|Print..&#x2e;</b> menu item in a GUI application. In such cases, a newly-constructed <a href="qprinter.html">QPrinter</a> object is supplied to a <a href="qprintdialog.html">QPrintDialog</a>, allowing the user to specify the printer to use, paper size, and other printing properties.</p>
<pre class="cpp">

          <span class="type"><a href="qprinter.html">QPrinter</a></span> printer;

          <span class="type"><a href="qprintdialog.html">QPrintDialog</a></span> dialog(<span class="operator">&amp;</span>printer<span class="operator">,</span> <span class="keyword">this</span>);
          dialog<span class="operator">.</span>setWindowTitle(tr(<span class="string">&quot;Print Document&quot;</span>));
          <span class="keyword">if</span> (editor<span class="operator">-</span><span class="operator">&gt;</span>textCursor()<span class="operator">.</span>hasSelection())
              dialog<span class="operator">.</span>addEnabledOption(<span class="type"><a href="qabstractprintdialog.html">QAbstractPrintDialog</a></span><span class="operator">::</span>PrintSelection);
          <span class="keyword">if</span> (dialog<span class="operator">.</span>exec() <span class="operator">!</span><span class="operator">=</span> <span class="type"><a href="../qtwidgets/qdialog.html">QDialog</a></span><span class="operator">::</span>Accepted) {
              <span class="keyword">return</span>;
          }

</pre>
<p>It is also possible to set certain default properties by modifying the <a href="qprinter.html">QPrinter</a> before it is supplied to the print dialog. For example, applications that generate batches of reports for printing may set up the <a href="qprinter.html">QPrinter</a> to <a href="qprinter.html#setOutputFileName">write to a local file</a> by default rather than to a printer.</p>
<a name="painting-onto-a-page"></a>
<h3 >Painting onto a Page</h3>
<p>Once a <a href="qprinter.html">QPrinter</a> object has been constructed and set up, a <a href="../qtgui/qpainter.html">QPainter</a> can be used to perform painting operations on it. We can construct and set up a painter in the following way:</p>
<pre class="cpp">

      <span class="type"><a href="qprinter.html">QPrinter</a></span> printer(<span class="type"><a href="qprinter.html">QPrinter</a></span><span class="operator">::</span>HighResolution);
      printer<span class="operator">.</span>setOutputFileName(<span class="string">&quot;print.ps&quot;</span>);
      <span class="type"><a href="../qtgui/qpainter.html">QPainter</a></span> painter;
      painter<span class="operator">.</span>begin(<span class="operator">&amp;</span>printer);

      <span class="keyword">for</span> (<span class="type">int</span> page <span class="operator">=</span> <span class="number">0</span>; page <span class="operator">&lt;</span> numberOfPages; <span class="operator">+</span><span class="operator">+</span>page) {

          <span class="comment">// Use the painter to draw on the page.</span>

          <span class="keyword">if</span> (page <span class="operator">!</span><span class="operator">=</span> lastPage)
              printer<span class="operator">.</span>newPage();
      }

      painter<span class="operator">.</span>end();

</pre>
<p>Since the <a href="qprinter.html">QPrinter</a> starts with a blank page, we only need to call the <a href="qprinter.html#newPage">newPage()</a> function after drawing each page, except for the last page.</p>
<p>The document is sent to the printer, or written to a local file, when we call <a href="../qtgui/qpainter.html#end">end()</a>.</p>
<a name="coordinate-systems"></a>
<h3 >Coordinate Systems</h3>
<p><a href="qprinter.html">QPrinter</a> provides functions that can be used to obtain information about the dimensions of the paper (the paper rectangle) and the dimensions of the printable area (the page rectangle). These are given in logical device coordinates that may differ from the physical coordinates used by the device itself, indicating that the printer is able to render text and graphics at a (typically higher) resolution than the user's display.</p>
<p>Although we do not need to handle the conversion between logical and physical coordinates ourselves, we still need to apply transformations to painting operations because the pixel measurements used to draw on screen are often too small for the higher resolutions of typical printers.</p>
<div class="table"><table class="generic">
 <tr valign="top" class="odd"><td ><b>Printer and Painter Coordinate Systems</b><p>The <a href="qprinter-obsolete.html#paperRect-1">paperRect()</a> and <a href="qprinter-obsolete.html#pageRect-1">pageRect()</a> functions provide information about the size of the paper used for printing and the area on it that can be painted on.</p>
<p>The rectangle returned by <a href="qprinter-obsolete.html#pageRect-1">pageRect()</a> usually lies inside the rectangle returned by <a href="qprinter-obsolete.html#paperRect-1">paperRect()</a>. You do not need to take the positions and sizes of these area into account when using a <a href="../qtgui/qpainter.html">QPainter</a> with a <a href="qprinter.html">QPrinter</a> as the underlying paint device; the origin of the painter's coordinate system will coincide with the top-left corner of the page rectangle, and painting operations will be clipped to the bounds of the drawable part of the page.</p>
</td><td ><img src="images/printer-rects.png" alt="" /></td></tr>
</table></div>
<p>The paint system automatically uses the correct device metrics when painting text but, if you need to position text using information obtained from font metrics, you need to ensure that the print device is specified when you construct <a href="../qtgui/qfontmetrics.html">QFontMetrics</a> and <a href="../qtgui/qfontmetricsf.html">QFontMetricsF</a> objects, or ensure that each <a href="../qtgui/qfont.html">QFont</a> used is constructed using the form of the constructor that accepts a <a href="../qtgui/qpaintdevice.html">QPaintDevice</a> argument.</p>
<a name="printing-widgets"></a>
<h2 id="printing-widgets">Printing Widgets</h2>
<p>To print a widget, you can use the <a href="../qtwidgets/qwidget.html#render">QWidget::render</a>() 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.</p>
<pre class="cpp">

          <span class="type"><a href="../qtgui/qpainter.html">QPainter</a></span> painter;
          painter<span class="operator">.</span>begin(<span class="operator">&amp;</span>printer);
          <span class="type">double</span> xscale <span class="operator">=</span> printer<span class="operator">.</span>pageRect()<span class="operator">.</span>width()<span class="operator">/</span><span class="type">double</span>(myWidget<span class="operator">-</span><span class="operator">&gt;</span>width());
          <span class="type">double</span> yscale <span class="operator">=</span> printer<span class="operator">.</span>pageRect()<span class="operator">.</span>height()<span class="operator">/</span><span class="type">double</span>(myWidget<span class="operator">-</span><span class="operator">&gt;</span>height());
          <span class="type">double</span> scale <span class="operator">=</span> <a href="../qtcore/qtglobal.html#qMin">qMin</a>(xscale<span class="operator">,</span> yscale);
          painter<span class="operator">.</span>translate(printer<span class="operator">.</span>paperRect()<span class="operator">.</span>x() <span class="operator">+</span> printer<span class="operator">.</span>pageRect()<span class="operator">.</span>width()<span class="operator">/</span><span class="number">2</span><span class="operator">,</span>
                             printer<span class="operator">.</span>paperRect()<span class="operator">.</span>y() <span class="operator">+</span> printer<span class="operator">.</span>pageRect()<span class="operator">.</span>height()<span class="operator">/</span><span class="number">2</span>);
          painter<span class="operator">.</span>scale(scale<span class="operator">,</span> scale);
          painter<span class="operator">.</span>translate(<span class="operator">-</span>width()<span class="operator">/</span><span class="number">2</span><span class="operator">,</span> <span class="operator">-</span>height()<span class="operator">/</span><span class="number">2</span>);

          myWidget<span class="operator">-</span><span class="operator">&gt;</span>render(<span class="operator">&amp;</span>painter);

</pre>
<p>This will center the widget on the page and scale it so that it fits the page.</p>
<a name="printing-from-complex-widgets"></a>
<h2 id="printing-from-complex-widgets">Printing from Complex Widgets</h2>
<p>Certain widgets, such as <a href="../qtwidgets/qtextedit.html">QTextEdit</a> and <a href="../qtwidgets/qgraphicsview.html">QGraphicsView</a>, display rich content that is typically managed by instances of other classes, such as <a href="../qtgui/qtextdocument.html">QTextDocument</a> and <a href="../qtwidgets/qgraphicsscene.html">QGraphicsScene</a>. As a result, it is these content handling classes that usually provide printing functionality, either via a function that can be used to perform the complete task, or via a function that accepts an existing <a href="../qtgui/qpainter.html">QPainter</a> object. Some widgets provide convenience functions to expose underlying printing features, avoiding the need to obtain the content handler just to call a single function.</p>
<p>The following table shows which class and function are responsible for printing from a selection of different widgets. For widgets that do not expose printing functionality directly, the content handling classes containing this functionality can be obtained via a function in the corresponding widget's API.</p>
<div class="table"><table class="generic">
 <thead><tr class="qt-style"><th >Widget</th><th >Printing function</th><th >Accepts</th></tr></thead>
<tr valign="top" class="odd"><td ><a href="../qtwidgets/qgraphicsview.html">QGraphicsView</a></td><td ><a href="../qtwidgets/qgraphicsview.html#render">QGraphicsView::render</a>()</td><td ><a href="../qtgui/qpainter.html">QPainter</a></td></tr>
<tr valign="top" class="even"><td >QSvgWidget</td><td >QSvgRenderer::render()</td><td ><a href="../qtgui/qpainter.html">QPainter</a></td></tr>
<tr valign="top" class="odd"><td ><a href="../qtwidgets/qtextedit.html">QTextEdit</a></td><td ><a href="../qtgui/qtextdocument.html#print">QTextDocument::print</a>()</td><td ><a href="qprinter.html">QPrinter</a></td></tr>
<tr valign="top" class="even"><td ><a href="../qtgui/qtextlayout.html">QTextLayout</a></td><td ><a href="../qtgui/qtextlayout.html#draw">QTextLayout::draw</a>()</td><td ><a href="../qtgui/qpainter.html">QPainter</a></td></tr>
<tr valign="top" class="odd"><td ><a href="../qtgui/qtextline.html">QTextLine</a></td><td ><a href="../qtgui/qtextline.html#draw">QTextLine::draw</a>()</td><td ><a href="../qtgui/qpainter.html">QPainter</a></td></tr>
</table></div>
<p><a href="../qtwidgets/qtextedit.html">QTextEdit</a> requires a <a href="qprinter.html">QPrinter</a> rather than a <a href="../qtgui/qpainter.html">QPainter</a> because it uses information about the configured page dimensions in order to insert page breaks at the most appropriate places in printed documents.</p>
</div>
<!-- @@@qtprintsupport-index.html -->
        </div>
       </div>
   </div>
   </div>
</div>
<div class="footer">
   <p>
   <acronym title="Copyright">&copy;</acronym> 2017 The Qt Company Ltd.
   Documentation contributions included herein are the copyrights of
   their respective owners.<br>    The documentation provided herein is licensed under the terms of the    <a href="http://www.gnu.org/licenses/fdl.html">GNU Free Documentation    License version 1.3</a> as published by the Free Software Foundation.<br>    Qt and respective logos are trademarks of The Qt Company Ltd.     in Finland and/or other countries worldwide. All other trademarks are property
   of their respective owners. </p>
</div>
</body>
</html>