jkriege2 / JKQtPlotter

an extensive Qt5 & Qt6 Plotter framework (including a feature-richt plotter widget, a speed-optimized, but limited variant and a LaTeX equation renderer!), written fully in C/C++ and without external dependencies
http://jkriege2.github.io/JKQtPlotter/index.html
GNU Lesser General Public License v2.1
889 stars 190 forks source link

Can't write to PDF files with JKQTPlotter::saveImage() when passing a filename ending in ".pdf" #111

Closed fpalazzolo closed 9 months ago

fpalazzolo commented 11 months ago

The documentation indicates that saveImage() should be able to write to pdf files. But, at least in my case on Windows, if you pass a filename like "test.pdf", the file is not written. The issue is that the variable "selFormat" in JKQTBasePlotter::saveImage() is only set if it is parsed out of the filesave dialog, which is skipped if the filename is already set:

    QString selFormat="";
    if (fn.isEmpty()) {
        selFormat=currentFileFormat;
        fn = QFileDialog::getSaveFileName(nullptr, tr("Save Plot"),
                                    currentSaveDirectory,
                                          filt.join(";;"), &selFormat);

and then later:

    if (!fn.isEmpty()) {
        int filtID=filt.indexOf(selFormat);

After this, filtID is -1, and things are not right from that point on. At this point, an invalid reference gets passed to JKQTBasePlotter::savePixelImage(), which has similar logic, and also finds filtID = -1. Then, the only reason the other formats work at all is that QImage.save() knows how to save all the image formats based on the extension.

I suspect SVG file saving is similarly broken for this use case.

My quick workaround was to use saveAsPDF() directly.

In the end, JKQTBasePlotter::saveImage() (and maybe JKQTBasePlotter::savePixelImage()) should probably be reviewed to insure that they work properly for all file types they support, when the filename is set prior to calling. I don't have time to do this now, but maybe later.

jkriege2 commented 11 months ago

Hi!

thanks for reporting ... I will lokk into this... but give me some time!

Best, JAN

jkriege2 commented 11 months ago

Hi!

I reworked these methods, so the issue should be fixed via https://github.com/jkriege2/JKQtPlotter/commit/995ca920332571d185c2828c0de0756683d82cef ...

Could you please verify?

Thanks, JAN

jkriege2 commented 10 months ago

@fpalazzolo: This should be resolved! Can I close the issue?

jkriege2 commented 9 months ago

This should be fixed. Please reopen if the problem persists, or post a new issue!

THANKS! JAN