SVG Generator Example▲
The SVG Generator example shows how to add SVG file export to applications.
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.
The example consists of two classes: Window and DisplayWidget.
The Window class contains the application logic and constructs the user interface from a Qt Designer UI file as described in the Qt Designer manual. It also contains the code to write an SVG file.
The DisplayWidget 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 Window class so that it can be used to generate SVG files.
The DisplayWidget Class▲
The DisplayWidget class displays a drawing consisting of a selection of elements chosen by the user. These are defined using Shape and Background enums that are included within the class definition:
class
DisplayWidget : public
QWidget
{
Q_OBJECT
public
:
enum
Shape {
House =
0
, Car =
1
}
;
enum
Background {
Sky =
0
, Trees =
1
, Road =
2
}
;
DisplayWidget(QWidget *
parent =
0
);
QColor color() const
;
void
paint(QPainter &
amp;painter);
public
slots:
void
setBackground(Background background);
void
setColor(const
QColor &
amp;color);
void
setShape(Shape shape);
protected
:
void
paintEvent(QPaintEvent *
event) override
;
private
:
Background background;
QColor shapeColor;
Shape shape;
QHash&
lt;Shape,QPainterPath&
gt; shapeMap;
QPainterPath moon;
QPainterPath tree;
}
;
Much of this class is used to configure the appearance of the drawing. The paintEvent() and paint() 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.
We reimplement the QWidget::paintEvent() function to display the drawing on screen:
void
DisplayWidget::
paintEvent(QPaintEvent *
/* event */
)
{
QPainter painter;
painter.begin(this
);
painter.setRenderHint(QPainter::
Antialiasing);
paint(painter);
painter.end();
}
Here, we only construct a QPainter object, begin painting on the device and set a render hint for improved output quality before calling the paint() function to perform the painting itself. When this returns, we close the painter and return.
The paint() function is designed to be used for different painting tasks. In this example, we use it to draw on a DisplayWidget instance and on a QSvgGenerator object. We show how the painting is performed to demonstrate that there is nothing device-specific about the process:
void
DisplayWidget::
paint(QPainter &
amp;painter)
{
painter.setClipRect(QRect(0
, 0
, 200
, 200
));
painter.setPen(Qt::
NoPen);
switch
(background) {
case
Sky:
default
:
painter.fillRect(QRect(0
, 0
, 200
, 200
), Qt::
darkBlue);
painter.translate(145
, 10
);
painter.setBrush(Qt::
white);
painter.drawPath(moon);
painter.translate(-
145
, -
10
);
break
;
case
Trees:
{
painter.fillRect(QRect(0
, 0
, 200
, 200
), Qt::
darkGreen);
painter.setBrush(Qt::
green);
painter.setPen(Qt::
black);
for
(