QCustomPlot Discussion and Comments

If QCustomPlot lost focus, savePng only save axiesReturn to overview

I am using QMdiSubWindow to show multi plot. I have a subWindowList to store QMdiSubWindow every QMdiSubWindow has a child widget PlotWindow, the PlotWindow is inheritd from QMainWindow, and every PlotWindow has a QCsutomPlot as its centralWidget. I am using MdiArea to show the PlotWIndow, so every time only one PlotWindow get focus. QCsutomPlot display works fine when I choose between these windows.The problem is saving plot to image file. I use following code to do the save work:

foreach (QMdiSubWindow *pSubWindow, subWindowList()) {
        PlotWindow *pPlotWindow = qobject_cast<PlotWindow*>(pSubWindow->widget());
        QString fileName = pPlotWindow->getCustomPlot()->windowTitle().replace(" ","");
        pPlotWindow->getCustomPlot()->savePng(fileName + ".png"));

Only the PlotWindow now get focus save the right image file(all the elements in customPlot), other PlotWindows saved file, but no plot and curve in the image( only have QCPPlotTitle, axies, QCPColorScale, just no plot and QCPCurve).

How can I save all these plot to image? QCustomPlot lost it plot if the application is not on it?

i would look at the documentation:


QCP is able to save plots also if the widget is obscured or invisible. The savePng/savePdf/... methods of QCP start a new run of the renderer, and don't just e.g. save a buffer/snapshow of the current display.
Try explicitly passing the size to the savePng method, so try for example

pPlotWindow->getCustomPlot()->savePng(fileName + ".png", 500, 400);

If this works, the issue is that Qt doesn't define the geometry of your sub windows properly (either because the subwindow is minimized or just constructed), and thus QCustomPlot has no proper width and height to work with. (If you don't pass the export size explicitly, it uses the current widget geometry.)
If this doesn't help, I'd be interested in a minimal example that reproduces this, which you could maybe send via the contact email.

I think the problem is more likely due to replot() using the widget's dimensions to do it's work. The above mentioned behavior can easily be reproduced why simply never calling show() on the QCustomPlot widget. I have run into this problem trying to save plots from a background thread, or running with only offscreen drawing.

Yeah, like I said in my previous post, this is what savePng etc. are for. The replot() method is intended for a visible plot widget, not background-rendering.

Thank you for the clarification. I misunderstood and thought that replot() was necessary even before calling savePng().

Ah I see, I'll try to make that clearer in the documentation.