Evgenii Legotckoi
March 20, 2016, 10:31 p.m.

Qt/C++ - Lesson 044. Saving objects from QGraphicsScene into SVG

Using Qt, you can save the contents of the graphic scene QGraphicsScene to SVG vector graphics files that are opened without problems after such editors as CorelDraw. I propose to write a small application that will save the contents of the graphic scenes in SVG file format, and then open it using CorelDraw.

Project structure

  • SvgExample.pro - Project Profile;
  • mainwindow.h - header file of the main application window;
  • mainwindow.cpp - source file of the main application window, in which all the action will take place;
  • mainwindow.ui - file forms the main application window;
  • main.cpp - the main boot file source.

mainwindow.ui

In the interface designer we creates button, which will open a dialog box in which we choose the path to save the SVG file. As well as set widget QGraphicsView, in which the graphic scene will be placed.

SvgExample.pro

The project profile is absolutely necessary to connect the SVG module to be able to work with classes of vector graphics.

  1. #-------------------------------------------------
  2. #
  3. # Project created by QtCreator 2016-03-09T23:52:59
  4. #
  5. #-------------------------------------------------
  6.  
  7. QT += core gui svg
  8.  
  9. greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
  10.  
  11. TARGET = SvgExample
  12. TEMPLATE = app
  13.  
  14.  
  15. SOURCES += main.cpp\
  16. mainwindow.cpp
  17.  
  18. HEADERS += mainwindow.h
  19.  
  20. FORMS += mainwindow.ui

main.cpp

  1. #include "mainwindow.h"
  2. #include <QApplication>
  3.  
  4. int main(int argc, char *argv[])
  5. {
  6. QApplication a(argc, argv);
  7. MainWindow w;
  8. w.show();
  9.  
  10. return a.exec();
  11. }

mainwindow.h

So, in the header of the main application window connect QGraphicsScene , QGraphicsRectItem , QSvgGenerator , QFileDialog and QPainter .

On the graphics it will create two rectangles that need to be saved in the SVG file. Saving will be made in the button pressing processing slot, which will cause a dialog box to select the ways to preserve the SVG file.

  1. #ifndef MAINWINDOW_H
  2. #define MAINWINDOW_H
  3.  
  4. #include <QMainWindow>
  5. #include <QGraphicsScene>
  6. #include <QGraphicsRectItem>
  7. #include <QSvgGenerator>
  8. #include <QFileDialog>
  9. #include <QPainter>
  10.  
  11. namespace Ui {
  12. class MainWindow;
  13. }
  14.  
  15. class MainWindow : public QMainWindow
  16. {
  17. Q_OBJECT
  18.  
  19. public:
  20. explicit MainWindow(QWidget *parent = 0);
  21. ~MainWindow();
  22.  
  23. private slots:
  24. void on_saveButton_clicked();
  25.  
  26. private:
  27. Ui::MainWindow *ui;
  28. QGraphicsScene *scene;
  29. QString path; // The path to save the file
  30. };
  31.  
  32. #endif // MAINWINDOW_H

mainwindow.cpp

To set the service parameters and draw graphic objects with a graphic scene used QSvgGenerator class object, but for data transfer and direct rendering in this object will be used by QPainter class object, which set a target to which the rendering will be made using the begin() method, and the purpose of rendering the object itself is directly QSvgGenerator class. Drawing will proizodit using the method of graphic scene render() , through QPainter .

  1. #include "mainwindow.h"
  2. #include "ui_mainwindow.h"
  3.  
  4. MainWindow::MainWindow(QWidget *parent) :
  5. QMainWindow(parent),
  6. ui(new Ui::MainWindow)
  7. {
  8. ui->setupUi(this);
  9.  
  10. scene = new QGraphicsScene();
  11. ui->graphicsView->setScene(scene);
  12. scene->setSceneRect(0,0,400,400);
  13.  
  14. // Create the first red rectangle on the graphic scene
  15. QGraphicsRectItem *rect1 = new QGraphicsRectItem();
  16. rect1->setRect(10,50,100,50);
  17. rect1->setBrush(QBrush(Qt::red));
  18. rect1->setPen(QPen(QBrush(Qt::black),2));
  19. scene->addItem(rect1);
  20.  
  21. // And create a second blue rectangle on the graphic scene
  22. QGraphicsRectItem *rect2 = new QGraphicsRectItem();
  23. rect2->setRect(150,100,115,75);
  24. rect2->setBrush(QBrush(Qt::blue));
  25. rect2->setPen(QPen(QBrush(Qt::black),2));
  26. scene->addItem(rect2);
  27. }
  28.  
  29. MainWindow::~MainWindow()
  30. {
  31. delete ui;
  32. }
  33.  
  34. void MainWindow::on_saveButton_clicked()
  35. {
  36. // Take file path and name that will create
  37. QString newPath = QFileDialog::getSaveFileName(this, trUtf8("Save SVG"),
  38. path, tr("SVG files (*.svg)"));
  39.  
  40. if (newPath.isEmpty())
  41. return;
  42.  
  43. path = newPath;
  44.  
  45. QSvgGenerator generator; // Create a file generator object
  46. generator.setFileName(path); // We set the path to the file where to save vector graphics
  47. generator.setSize(QSize(scene->width(), scene->height())); // Set the dimensions of the working area of the document in millimeters
  48. generator.setViewBox(QRect(0, 0, scene->width(), scene->height())); // Set the work area in the coordinates
  49. generator.setTitle(trUtf8("SVG Example")); // The title document
  50. generator.setDescription(trUtf8("File created by SVG Example"));
  51.  
  52. QPainter painter;
  53. painter.begin(&generator);
  54. scene->render(&painter);
  55. painter.end();
  56.  
  57. // At the end we get a vector drawing file with the contents of the graphic scenes
  58. }

Result

As a result of this code you will get the SVG file that successfully opens in CorelDraw.

  1. SvgReader on the Qt. Loading data from SVG file into QGraphicsScene

Video

N
  • June 1, 2017, 4:13 a.m.

Добрый вечер. Встал вопрос. Каким образом сохранить координаты объектов, потому что в следующем уроке объекты на сцене будут находиться все в одном месте из которого их еще придется перетаскивать

Evgenii Legotckoi
  • June 1, 2017, 12:29 p.m.

Координаты объектов в данном случае нормально сохраняются. В конце статьи есть урок по восстановлению данных из SVG файла, изучите его. Там показано, как расположить объекты на графической сцене во время восстановления данных.

АО
  • March 13, 2020, 3:20 p.m.

Здравствуйте!
Как можно добавить текст туда?
Хочу попробовать нарисовать граф и обозначить вершины с дугами.

Добрый день.
Текст можно добавить на графическубюю сцену с помощью QGraphicsTextItem.

МЧ
  • May 18, 2020, 12:12 a.m.

Добрый день!
А не подскажете как можно сохранить фон, если это моя собственная картинка? При сохранении просто однотонного фона, не картинки все работает хорошо.

Evgenii Legotckoi
  • May 18, 2020, 1:31 p.m.

Добрый день.
Не подскажу. Могу только направить. Для это нужно в каком-нибудь популярном svg редакторе смотреть, как будет сохраняться ихображение, и потом попытаться повторить тоже самое средствами Qt.
Вполне возможно, что изображение там или отдельно будет сохраняться или будет как-то встраиваться в SVG файл.

Comments

Only authorized users can post comments.
Please, Log in or Sign up
  • Last comments
  • Evgenii Legotckoi
    March 9, 2025, 9:02 p.m.
    К сожалению, я этого подсказать не могу, поскольку у меня нет необходимости в обходе блокировок и т.д. Поэтому я и не задавался решением этой проблемы. Ну выглядит так, что вам действитель…
  • VP
    March 9, 2025, 4:14 p.m.
    Здравствуйте! Я устанавливал Qt6 из исходников а также Qt Creator по отдельности. Все компоненты, связанные с разработкой для Android, установлены. Кроме одного... Когда пытаюсь скомпилиров…
  • ИМ
    Nov. 22, 2024, 9:51 p.m.
    Добрый вечер Евгений! Я сделал себе авторизацию аналогичную вашей, все работает, кроме возврата к предидущей странице. Редеректит всегда на главную, хотя в логах сервера вижу запросы на правильн…
  • Evgenii Legotckoi
    Oct. 31, 2024, 11:37 p.m.
    Добрый день. Да, можно. Либо через такие же плагины, либо с постобработкой через python библиотеку Beautiful Soup
  • A
    Oct. 19, 2024, 5:19 p.m.
    Подскажите как это запустить? Я не шарю в программировании и кодинге. Скачал и установаил Qt, но куча ошибок выдается и не запустить. А очень надо fb3 переконвертировать в html