I'm trying to learn how to use the drawing functions. Till now i have been able to use the QPainter class to draw some circles on a QWidget like this:
in MainWindow.h i added:
virtual void paintEvents(QPaintEvent *event);
Then in MainWindow.cpp i added:
void MainWindow::paintEvents(QPaintEvent *event) {
QPainter painter(this);
painter.drawEllipse( 305, 55, 475, 475 );
painter.drawEllipse( 320, 70, 445, 445 );
painter.end();
}
This draws me some circles in pixels, but i need svg. So how can i use the QPainter class to draw svg circles instead?
You can use Qt SVG module. If you are using qmake, add QT += svg
to your .pro
file and then you will be able to use SVG classes.
Then, you can utilize QSvgRenderer to draw svg documents with your QPainter instance like:
#include <QSvgRenderer>
...
void MainWindow::paintEvents(QPaintEvent *event) {
QPainter painter(this);
QSvgRenderer svgr("/path/to/img.svg");
svgr.render(&painter);
painter.end();
}
As per documentation render
method has two other overloads which will give you control over where and what to render.
You may also want to load your svg content from QByteArray or XML stream which have their appropriate constructors or load methods.