添加
#include <QtSvg/QSvgGenerator>
#include <QFileDialog>
QT += core gui svg
一些重要的头文件以及声明。
创建menu bar, 命名一个Save as SVG,添加一个slog,源码如下;
#include "basicshapewindow.h"
#include "ui_basicshapewindow.h"
#include <QtSvg/QSvgGenerator>
#include <QFileDialog>
BasicShapeWindow::BasicShapeWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::BasicShapeWindow)
{
ui->setupUi(this);
}
void BasicShapeWindow::paintEvent(QPaintEvent *event)
{
paintAll();
}
void BasicShapeWindow::paintAll(QSvgGenerator *generator)
{
QPainter painter;
if (generator)
painter.begin(generator);
else
painter.begin(this);
painter.setFont(QFont("Times", 14, QFont::Bold));
painter.drawText(QPoint(20, 30), "Testing");
painter.drawLine(QPoint(50, 60), QPoint(100, 100));
painter.setBrush(Qt::BDiag