Evgenii Legotckoi
Dec. 23, 2015, 8:36 p.m.

Qt/C++ - Lesson 032. Saving picture in file from QCustomPlot

In a previous article on working with QCustomPlot was an example of the use of this library. But after the schedule is built, it may be necessary to make its preservation as a graphic file. QCustomPlot Library already provides methods necessary for this. All you need to do - is to create a file which will be saved and pass the path to the file in one of the methods to save the image.

Methods for saving images

  1. saveRastered(const QString &fileName, int width, int height, double scale, const char *format, int quality)
  2. saveJpg(const QString &fileName, int width, int height, double scale, int quality);
  3. savePng(const QString &fileName, int width, int height, double scale, int quality);
  4. saveBmp(const QString &fileName, int width, int height, double scale);
  5. savePdf(const QString &fileName, bool noCosmeticPen, int width, int height, const QString &pdfCreator, const QString &pdfTitle);

saveRastered

It is a fundamental method for methods: saveJpg , savePng , saveBmp . So he called in all of these methods. Inside the method saveRastered QPixmap instance is created, through which the image is saved from QCustomPlot widget. For this method, you can specify the type of file to be saved. Then the remaining three methods simplify work and just write code, canceling the need to specify the file type.

savePdf

This method stands alone and does not use QPixmap , and runs through QPrinter . Although the use of logic in the minimum variant is similar to the rest of the methods.


Save image

Extend the code from the previous tutorial by adding the Save button in the slot where the preservation of the image will be in the file in the following path C:/example/customPlot.png.

In addition to the path to the file, you can specify image quality, its scale, and the parameters of the parties.

  1. void MainWindow::on_pushButton_clicked()
  2. {
  3. QString fileName("C:/example/customPlot.png");
  4. QFile file(fileName);
  5.  
  6. if (!file.open(QIODevice::WriteOnly))
  7. {
  8. qDebug() << file.errorString();
  9. } else {
  10. wGraphic->savePng(fileName);
  11. }
  12. }

Do you like it? Share on social networks!

Comments

Only authorized users can post comments.
Please, Log in or Sign up
  • Last comments
  • AK
    April 1, 2025, 11:41 a.m.
    Добрый день. В данный момент работаю над проектом, где необходимо выводить звук из программы в определенное аудиоустройство (колонки, наушники, виртуальный кабель и т.д). Пишу на Qt5.12.12 поско…
  • 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