Important: Please read the Qt Code of Conduct -

QPainter.begin returns false on prod machine

  • Hi everyone,

    I have an issue using QPrinter and QPainter in my application : I want to print a QWidget as a PDF file. It works perfectly on my development machine, but on the Prod machine, it doesn't. So I guess I have a configuration problem.

    Here is the guity piece of code :

    QPrinter l_oPrinter;
    l_oPrinter.setOutputFileName(/* something */);
    l_oPrinter.setPageMargins(50, 50, 50, 50, QPrinter::DevicePixel);
    // l_oPrinter.isValid() returns true
    QPainter l_oPainter;
    if ( l_oPainter.begin(&l_oPrinter) ) // returns false on Prod machine, true on development one
    	ui.wdg_All->render(&l_oPainter); // ui.wdg_All is the QWidget I want to print

    My app is a 64bits C++ Windows 7 program running with Qt 5.2.1 (I use Visual Studio 2012 as dev env, not Qt Creator). I first realized that on the Prod machine, no printer was installed. So I installed a random laser one, and even PDFCreator. But nothing changed.

    Then I realized that QPrinterInfo::availablePrinters() returned an empty list on the Prod machine, while it returned all the actually available printers on my machine. I searched on forums and found that I was missing printsupport/windowsprintersupport.dll in my application folder. So I added it and now the returned printers list is fine, but the problem is not solved : I still get "false" returned from the begin() method.

    I checked the dependencies : nothing to declare. I have Qt5PrintSupport.dll correctly used. No more dll missing, or at least, nothing is telling me the opposite.

    Any idea anyone?

  • I think thats a dependency problem, anyhow.

    Are you using windeployqt.exe tool ?

    If not look here :

    To check wether there are dlls missing you can use DependencyWalker :

  • @euchkatzl

    Thansk, I am not using windeployqt.exe, I am gonna have a look.

    Anyway I do use DependencyWalker, and that's the problem : it doesn't give me any wrong feedback, all is green...

    I have even tried to copy all the Qt dlls into my app folder (hardcore attempt when you suspect that you are missing a dll), but nothing changed.

  • Moderators

    make sure the printer plugins are found and successfully loaded.
    Set QT_DEBUG_PLUGINS env variable to a non-zero and check the console output.
    Note: plugins are searched in special-named folders.

  • @raven-worx

    The only plugin that seems to be used is windowsprintersupport.dll, and it's correctly loaded from the special-named folder "printsupport"

  • I dont think this a QPrinter problem but more a QPainter problem.

    As a notice you don't need any printers to be installed on your prod machine.
    You want to write to an PDF file and not print the resulting document. Is this right ?

    Try deploying the imageformats that windeployqt.exe gives you.

  • @euchkatzl

    Correct, this is what I want it to do.

    I tried to use the deploying tool, so I got imageformats dlls + other files added ( some files.qm, D3Dcompiler46.dll, accessible and iconengines folders containing other dlls...), but it doesn't solve the problem.

  • I found part of the problem :

    Executing the application with user Windows session fails, BUT executing it with admin session works...

    Now I have to understand why, and how to fix that. Looks like the lambda user session can't access any printer from Qt.

  • @PierreFIL

    When I do this I use code like this...

    QPainter l_oPainter(&l_oPrinter);

    I think the newPage() line would also work just before the call to render.
    Painter and printer then goes out of scope in my code. How about yours, does this not work for you?

  • @kenchan

    It works too yes, but I don't think it's related to newPage() method

  • OK, so now that I have launched the program with admin rights and came back with user session, the application is working (?!?)

    So it was neither a DLL or plugins problem, but just a weird right access blocking. I am sad I can't explain why I had to launch it once with admin session, but at least I am glad I solved my problem.

    Thanks anyway to those who give their time to help.