Save canvas to pdf in standalone code

Hi Everyone,

I am using the “simple_canvas” code by @bellenot.

Everything works fine until I want to save the content from the canvas to a vector graphic supported file type (e.g. pdf) using the “saveas” command.
The generated pdf file has a white background, which size is changeable with “SetWindowSize()”, but the histogram is missing.

My guess is, that there is a conflict between the TApplication and the QApplication because in the following example the histogram is drawn in the first pdf and not in the second one. Is this a known issue and exists therefor a workaround?


int main(int argc, char *argv[]) {
    TApplication *rootapp = new TApplication("Simple Qt ROOT Application", &argc, argv);

    TCanvas *canvas = new TCanvas("canvas", "canvas title", 800, 600);

    TFormula *form1 = new TFormula("form1","abs(sin(x)/x)");

    TF1 *sqroot = new TF1("sqroot","x*gaus(0) + [3]*form1", 0, 10);
    sqroot->SetParameters(10, 4, 1, 20);
    TH1F *hist = new TH1F("h1f","Test random numbers", 200, 0, 10);
    hist->FillRandom("sqroot", 100000);


    QApplication app(argc,argv);


    QMainCanvas m(0);

    m.setWindowTitle("Qt Example - Canvas");
    m.setGeometry( 100, 100, 699, 499 );;
    m.resize(700, 500);

    QObject::connect( qApp, SIGNAL(lastWindowClosed()), qApp, SLOT(quit()) );

    return app.exec();

ROOT: Version: 6.06.02
Platform: Not Provided
Compiler: Not Provided

Saving with SaveAs works for me with ROOT v5.34, I’ll need time to try with ROOT 6…

I have still the same issue using ROOT 5.34.23, 5.34.28 and 5.34.36.

With the simple_canvas example? It works just fine for me…

Unfortunately yes. If I choose png as file type, I get the desired output.

Well, here is what I get with ROOT v5.34/39, Visual Studio 2012, and Qt 5.5.1: tesdt.pdf (15.4 KB)
And the one with ROOT v6.19/01, Visual Studio 2019, and Qt 5.12.2: test2.pdf (15.4 KB)

Thanks a lot. I am using Qt 5.11.2 with the above mentioned ROOT versions on ubuntu 14.04. Also, I get the same empty pdf on ubuntu 16.04.

Since the code is correct, I assume my ROOT or Qt installation is faulty. Do I need some special build options for ROOT or Qt?

Then it might be a platform dependent issue. I’ll try on a Linux box and let you know.


Here is what I get with ROOT 6.19/01, gcc 6.2.0, Qt 5.9.2 on CentOS 7: testcc7.pdf (15.4 KB)

I noticed a strange behavior, maybe this can help: In the embedded pdf viewer from Visual Studio Code I get a non-empty pdf, see the attached screenshot.

But, it is only black and white.
If I open it in several other pdf viewers, it is still the same result. hist.pdf (15.3 KB)

I see the same thing with your file… Unfortunately our graphics expert, @couet is away and I can’t tell what is wrong with this pdf…

Oh wow, changing the system language from German to English solves the problem. After investigating a svg file with an editor, I noticed the wrong decimal separator.

Thanks for your support.

Wow! Thanks, good to know! (and weird side effect…)

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.