<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE html> <html lang="en"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <!-- svggenerator.qdoc --> <title>SVG Generator Example | Qt SVG 5.11</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 ><a href="../qtdoc/index.html">Qt 5.11</a></td><td ><a href="qtsvg-index.html">Qt SVG</a></td><td >SVG Generator Example</td></tr></table><table class="buildversion"><tr> <td id="buildversion" width="100%" align="right">Qt 5.11.2 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="#the-displaywidget-class">The DisplayWidget Class</a></li> <li class="level1"><a href="#the-window-class">The Window Class</a></li> <li class="level1"><a href="#further-reading">Further Reading</a></li> </ul> </div> <div class="sidebar-content" id="sidebar-content"></div></div> <h1 class="title">SVG Generator Example</h1> <span class="subtitle"></span> <!-- $$$svggenerator-description --> <div class="descr"> <a name="details"></a> <p>The SVG Generator example shows how to add SVG file export to applications.</p> <p class="centerAlign"><img src="images/svggenerator-example.png" alt="" /></p><p>Scalable Vector Graphics (SVG) is an XML-based language for describing two-dimensional vector graphics. Qt provides classes for rendering and generating SVG drawings. This example allows the user to create a simple picture and save it to an SVG file.</p> <p>The example consists of two classes: <code>Window</code> and <code>DisplayWidget</code>.</p> <p>The <code>Window</code> class contains the application logic and constructs the user interface from a Qt Designer UI file as described in the <a href="../qtdesigner/designer-using-a-ui-file.html#the-multiple-inheritance-approach">Qt Designer manual</a>. It also contains the code to write an SVG file.</p> <p>The <code>DisplayWidget</code> class performs all the work of painting a picture on screen. Since we want the SVG to resemble this picture as closely as possible, we make this code available to the <code>Window</code> class so that it can be used to generate SVG files.</p> <a name="the-displaywidget-class"></a> <h2 id="the-displaywidget-class">The DisplayWidget Class</h2> <p>The <code>DisplayWidget</code> class displays a drawing consisting of a selection of elements chosen by the user. These are defined using <code>Shape</code> and <code>Background</code> enums that are included within the class definition:</p> <pre class="cpp"> <span class="keyword">class</span> DisplayWidget : <span class="keyword">public</span> <span class="type"><a href="../qtwidgets/qwidget.html">QWidget</a></span> { Q_OBJECT <span class="keyword">public</span>: <span class="keyword">enum</span> Shape { House <span class="operator">=</span> <span class="number">0</span><span class="operator">,</span> Car <span class="operator">=</span> <span class="number">1</span> }; <span class="keyword">enum</span> Background { Sky <span class="operator">=</span> <span class="number">0</span><span class="operator">,</span> Trees <span class="operator">=</span> <span class="number">1</span><span class="operator">,</span> Road <span class="operator">=</span> <span class="number">2</span> }; DisplayWidget(<span class="type"><a href="../qtwidgets/qwidget.html">QWidget</a></span> <span class="operator">*</span>parent <span class="operator">=</span> <span class="number">0</span>); <span class="type"><a href="../qtgui/qcolor.html">QColor</a></span> color() <span class="keyword">const</span>; <span class="type">void</span> paint(<span class="type"><a href="../qtgui/qpainter.html">QPainter</a></span> <span class="operator">&</span>painter); <span class="keyword">public</span> <span class="keyword">slots</span>: <span class="type">void</span> setBackground(Background background); <span class="type">void</span> setColor(<span class="keyword">const</span> <span class="type"><a href="../qtgui/qcolor.html">QColor</a></span> <span class="operator">&</span>color); <span class="type">void</span> setShape(Shape shape); <span class="keyword">protected</span>: <span class="type">void</span> paintEvent(<span class="type"><a href="../qtgui/qpaintevent.html">QPaintEvent</a></span> <span class="operator">*</span>event) override; <span class="keyword">private</span>: Background background; <span class="type"><a href="../qtgui/qcolor.html">QColor</a></span> shapeColor; Shape shape; <span class="type"><a href="../qtcore/qhash.html">QHash</a></span><span class="operator"><</span>Shape<span class="operator">,</span><span class="type"><a href="../qtgui/qpainterpath.html">QPainterPath</a></span><span class="operator">></span> shapeMap; <span class="type"><a href="../qtgui/qpainterpath.html">QPainterPath</a></span> moon; <span class="type"><a href="../qtgui/qpainterpath.html">QPainterPath</a></span> tree; }; </pre> <p>Much of this class is used to configure the appearance of the drawing. The <code>paintEvent()</code> and <code>paint()</code> functions are most relevant to the purpose of this example, so we will describe these here and leave the reader to look at the source code for the example to see how shapes and colors are handled.</p> <p>We reimplement the <a href="../qtwidgets/qwidget.html#paintEvent">QWidget::paintEvent</a>() function to display the drawing on screen:</p> <pre class="cpp"> <span class="type">void</span> DisplayWidget<span class="operator">::</span>paintEvent(<span class="type"><a href="../qtgui/qpaintevent.html">QPaintEvent</a></span> <span class="operator">*</span> <span class="comment">/* event */</span>) { <span class="type"><a href="../qtgui/qpainter.html">QPainter</a></span> painter; painter<span class="operator">.</span>begin(<span class="keyword">this</span>); painter<span class="operator">.</span>setRenderHint(<span class="type"><a href="../qtgui/qpainter.html">QPainter</a></span><span class="operator">::</span>Antialiasing); paint(painter); painter<span class="operator">.</span>end(); } </pre> <p>Here, we only construct a <a href="../qtgui/qpainter.html">QPainter</a> object, begin painting on the device and set a render hint for improved output quality before calling the <code>paint()</code> function to perform the painting itself. When this returns, we close the painter and return.</p> <p>The <code>paint()</code> function is designed to be used for different painting tasks. In this example, we use it to draw on a <code>DisplayWidget</code> instance and on a <a href="qsvggenerator.html">QSvgGenerator</a> object. We show how the painting is performed to demonstrate that there is nothing device-specific about the process:</p> <pre class="cpp"> <span class="type">void</span> DisplayWidget<span class="operator">::</span>paint(<span class="type"><a href="../qtgui/qpainter.html">QPainter</a></span> <span class="operator">&</span>painter) { painter<span class="operator">.</span>setClipRect(<span class="type"><a href="../qtcore/qrect.html">QRect</a></span>(<span class="number">0</span><span class="operator">,</span> <span class="number">0</span><span class="operator">,</span> <span class="number">200</span><span class="operator">,</span> <span class="number">200</span>)); painter<span class="operator">.</span>setPen(<span class="type"><a href="../qtcore/qt.html">Qt</a></span><span class="operator">::</span>NoPen); <span class="keyword">switch</span> (background) { <span class="keyword">case</span> Sky: <span class="keyword">default</span>: painter<span class="operator">.</span>fillRect(<span class="type"><a href="../qtcore/qrect.html">QRect</a></span>(<span class="number">0</span><span class="operator">,</span> <span class="number">0</span><span class="operator">,</span> <span class="number">200</span><span class="operator">,</span> <span class="number">200</span>)<span class="operator">,</span> <span class="type"><a href="../qtcore/qt.html">Qt</a></span><span class="operator">::</span>darkBlue); painter<span class="operator">.</span>translate(<span class="number">145</span><span class="operator">,</span> <span class="number">10</span>); painter<span class="operator">.</span>setBrush(<span class="type"><a href="../qtcore/qt.html">Qt</a></span><span class="operator">::</span>white); painter<span class="operator">.</span>drawPath(moon); painter<span class="operator">.</span>translate(<span class="operator">-</span><span class="number">145</span><span class="operator">,</span> <span class="operator">-</span><span class="number">10</span>); <span class="keyword">break</span>; <span class="keyword">case</span> Trees: { painter<span class="operator">.</span>fillRect(<span class="type"><a href="../qtcore/qrect.html">QRect</a></span>(<span class="number">0</span><span class="operator">,</span> <span class="number">0</span><span class="operator">,</span> <span class="number">200</span><span class="operator">,</span> <span class="number">200</span>)<span class="operator">,</span> <span class="type"><a href="../qtcore/qt.html">Qt</a></span><span class="operator">::</span>darkGreen); painter<span class="operator">.</span>setBrush(<span class="type"><a href="../qtcore/qt.html">Qt</a></span><span class="operator">::</span>green); painter<span class="operator">.</span>setPen(<span class="type"><a href="../qtcore/qt.html">Qt</a></span><span class="operator">::</span>black); <span class="keyword">for</span> (<span class="type">int</span> y <span class="operator">=</span> <span class="operator">-</span><span class="number">55</span><span class="operator">,</span> row <span class="operator">=</span> <span class="number">0</span>; y <span class="operator"><</span> <span class="number">200</span>; y <span class="operator">+</span><span class="operator">=</span> <span class="number">50</span><span class="operator">,</span> <span class="operator">+</span><span class="operator">+</span>row) { <span class="type">int</span> xs; <span class="keyword">if</span> (row <span class="operator">=</span><span class="operator">=</span> <span class="number">2</span> <span class="operator">|</span><span class="operator">|</span> row <span class="operator">=</span><span class="operator">=</span> <span class="number">3</span>) xs <span class="operator">=</span> <span class="number">150</span>; <span class="keyword">else</span> xs <span class="operator">=</span> <span class="number">50</span>; <span class="keyword">for</span> (<span class="type">int</span> x <span class="operator">=</span> <span class="number">0</span>; x <span class="operator"><</span> <span class="number">200</span>; x <span class="operator">+</span><span class="operator">=</span> xs) { painter<span class="operator">.</span>save(); painter<span class="operator">.</span>translate(x<span class="operator">,</span> y); painter<span class="operator">.</span>drawPath(tree); painter<span class="operator">.</span>restore(); } } <span class="keyword">break</span>; } <span class="keyword">case</span> Road: painter<span class="operator">.</span>fillRect(<span class="type"><a href="../qtcore/qrect.html">QRect</a></span>(<span class="number">0</span><span class="operator">,</span> <span class="number">0</span><span class="operator">,</span> <span class="number">200</span><span class="operator">,</span> <span class="number">200</span>)<span class="operator">,</span> <span class="type"><a href="../qtcore/qt.html">Qt</a></span><span class="operator">::</span>gray); painter<span class="operator">.</span>setPen(<span class="type"><a href="../qtgui/qpen.html">QPen</a></span>(<span class="type"><a href="../qtcore/qt.html">Qt</a></span><span class="operator">::</span>white<span class="operator">,</span> <span class="number">4</span><span class="operator">,</span> <span class="type"><a href="../qtcore/qt.html">Qt</a></span><span class="operator">::</span>DashLine)); painter<span class="operator">.</span>drawLine(<span class="type"><a href="../qtcore/qline.html">QLine</a></span>(<span class="number">0</span><span class="operator">,</span> <span class="number">35</span><span class="operator">,</span> <span class="number">200</span><span class="operator">,</span> <span class="number">35</span>)); painter<span class="operator">.</span>drawLine(<span class="type"><a href="../qtcore/qline.html">QLine</a></span>(<span class="number">0</span><span class="operator">,</span> <span class="number">165</span><span class="operator">,</span> <span class="number">200</span><span class="operator">,</span> <span class="number">165</span>)); <span class="keyword">break</span>; } painter<span class="operator">.</span>setBrush(shapeColor); painter<span class="operator">.</span>setPen(<span class="type"><a href="../qtcore/qt.html">Qt</a></span><span class="operator">::</span>black); painter<span class="operator">.</span>translate(<span class="number">100</span><span class="operator">,</span> <span class="number">100</span>); painter<span class="operator">.</span>drawPath(shapeMap<span class="operator">[</span>shape<span class="operator">]</span>); } </pre> <a name="the-window-class"></a> <h2 id="the-window-class">The Window Class</h2> <p>The <code>Window</code> class represents the example's window, containing the user interface, which has been created using Qt Designer:</p> <pre class="cpp"> <span class="keyword">class</span> Window : <span class="keyword">public</span> <span class="type"><a href="../qtwidgets/qwidget.html">QWidget</a></span><span class="operator">,</span> <span class="keyword">private</span> Ui<span class="operator">::</span>Window { Q_OBJECT <span class="keyword">public</span>: Window(<span class="type"><a href="../qtwidgets/qwidget.html">QWidget</a></span> <span class="operator">*</span>parent <span class="operator">=</span> <span class="number">0</span>); <span class="keyword">public</span> <span class="keyword">slots</span>: <span class="type">void</span> saveSvg(); <span class="type">void</span> updateBackground(<span class="type">int</span> background); <span class="type">void</span> updateColor(); <span class="type">void</span> updateShape(<span class="type">int</span> shape); <span class="keyword">private</span>: <span class="type"><a href="../qtcore/qstring.html">QString</a></span> path; }; </pre> <p>As with the <code>DisplayWidget</code> class, we concentrate on the parts of the code which are concerned with painting and SVG generation. In the <code>Window</code> class, the <code>saveSvg()</code> function is called whenever the <b>Save As...</b> button is clicked; this connection was defined in the <code>window.ui</code> file using Qt Designer.</p> <p>The start of the <code>saveSvg()</code> function performs the task of showing a file dialog so that the user can specify a SVG file to save the drawing to.</p> <pre class="cpp"> <span class="type">void</span> Window<span class="operator">::</span>saveSvg() { <span class="type"><a href="../qtcore/qstring.html">QString</a></span> newPath <span class="operator">=</span> <span class="type"><a href="../qtwidgets/qfiledialog.html">QFileDialog</a></span><span class="operator">::</span>getSaveFileName(<span class="keyword">this</span><span class="operator">,</span> tr(<span class="string">"Save SVG"</span>)<span class="operator">,</span> path<span class="operator">,</span> tr(<span class="string">"SVG files (*.svg)"</span>)); <span class="keyword">if</span> (newPath<span class="operator">.</span>isEmpty()) <span class="keyword">return</span>; path <span class="operator">=</span> newPath; <span class="type"><a href="qsvggenerator.html">QSvgGenerator</a></span> generator; generator<span class="operator">.</span>setFileName(path); generator<span class="operator">.</span>setSize(<span class="type"><a href="../qtcore/qsize.html">QSize</a></span>(<span class="number">200</span><span class="operator">,</span> <span class="number">200</span>)); generator<span class="operator">.</span>setViewBox(<span class="type"><a href="../qtcore/qrect.html">QRect</a></span>(<span class="number">0</span><span class="operator">,</span> <span class="number">0</span><span class="operator">,</span> <span class="number">200</span><span class="operator">,</span> <span class="number">200</span>)); generator<span class="operator">.</span>setTitle(tr(<span class="string">"SVG Generator Example Drawing"</span>)); generator<span class="operator">.</span>setDescription(tr(<span class="string">"An SVG drawing created by the SVG Generator "</span> <span class="string">"Example provided with Qt."</span>)); <span class="type"><a href="../qtgui/qpainter.html">QPainter</a></span> painter; painter<span class="operator">.</span>begin(<span class="operator">&</span>generator); displayWidget<span class="operator">-</span><span class="operator">></span>paint(painter); painter<span class="operator">.</span>end(); } </pre> <p>In the rest of the function, we set up the generator and configure it to generate output with the appropriate dimensions and write to the user-specified file. We paint on the <a href="qsvggenerator.html">QSvgGenerator</a> object in the same way that we paint on a widget, calling the <code>DisplayWidget::paint()</code> function so that we use exactly the same code that we used to display the drawing.</p> <p>The generation process itself begins with the call to the painter's <a href="../qtgui/qpainter.html#begin">begin()</a> function and ends with call to its <a href="../qtgui/qpainter.html#end">end()</a> function. The <a href="qsvggenerator.html">QSvgGenerator</a> paint device relies on the explicit use of these functions to ensure that output is written to the file.</p> <a name="further-reading"></a> <h2 id="further-reading">Further Reading</h2> <p>The <a href="qtsvg-svgviewer-example.html">SVG Viewer Example</a> shows how to display SVG drawings in an application, and can be used to show the contents of SVG files created by this example.</p> <p>See the Qt SVG module documentation for more information about SVG and Qt's SVG classes.</p> <p>Files:</p> <ul> <li><a href="qtsvg-svggenerator-displaywidget-cpp.html">svggenerator/displaywidget.cpp</a></li> <li><a href="qtsvg-svggenerator-displaywidget-h.html">svggenerator/displaywidget.h</a></li> <li><a href="qtsvg-svggenerator-forms-window-ui.html">svggenerator/forms/window.ui</a></li> <li><a href="qtsvg-svggenerator-main-cpp.html">svggenerator/main.cpp</a></li> <li><a href="qtsvg-svggenerator-svggenerator-pro.html">svggenerator/svggenerator.pro</a></li> <li><a href="qtsvg-svggenerator-svggenerator-qrc.html">svggenerator/svggenerator.qrc</a></li> <li><a href="qtsvg-svggenerator-window-cpp.html">svggenerator/window.cpp</a></li> <li><a href="qtsvg-svggenerator-window-h.html">svggenerator/window.h</a></li> </ul> </div> <!-- @@@svggenerator --> </div> </div> </div> </div> </div> <div class="footer"> <p> <acronym title="Copyright">©</acronym> 2018 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>