QPrinter crash application

  • Setting "setPaperSize" crash application and show this message in console:

    bq. QWin32PrintEngine::initialize: CreateDC failed ()
    QWin32PrintEngine::initialize: CreateDC failed ()
    Error - RtlWerpReportException failed with status code :-1073741823. Will try to launch the process directly


    @MainWindow::MainWindow(QWidget *parent) :
    ui(new Ui::MainWindow)
    QObject::connect(ui->printButton, SIGNAL(clicked()), this, SLOT(printAction()));

    void MainWindow::printAction() {
    QPrinter p;
    p.setPaperSize(QPrinter::A4);//Crash application

    Without "setPaperSize" work fine, but using "QPrintPreviewDialog" and click in "Page setup" button, application crash too and show this error (same error):

    bq. QWin32PrintEngine::initialize: CreateDC failed ()
    QWin32PrintEngine::initialize: CreateDC failed ()
    Error - RtlWerpReportException failed with status code :-1073741823. Will try to launch the process directly

    perhaps the error occurs for the same reason.

    Image (Page setup button):

    !http://i57.tinypic.com/16jituf.png(qt5 page setup)!

    Is it a bug or I did something wrong?

  • from Qt documentation:

    void QPrinter::setPaperSize ( PaperSize newPaperSize )
    Sets the printer paper size to newPaperSize if that size is supported. The result is undefined if newPaperSize is not supported.

    The default paper size is driver-dependent.

    This function is useful mostly for setting a default value that the user can override in the print dialog.

    This function was introduced in Qt 4.4.

  • Lifetime Qt Champion


    This function has been obsoleted, you should rather use the new setPageSize

  • [quote author="SGaist" date="1421357569"]Hi,

    This function has been obsoleted, you should rather use the new setPageSize[/quote]

    In docs ( http://qt-project.org/doc/qt-4.8/qprinter-obsolete.html ) setPageSize is obsolete:

    bq. void QPrinter::setPageSize ( PageSize newPageSize )

    Sets the printer page size based on newPageSize.

    Use setPaperSize() instead.

    But I tried, and "p.setPageSize(QPrinter::A4);" crash too.


    @ QPrinter p;
    qDebug() << 1;
    qDebug() << 2;
    qDebug() << 3;

    QPrintPreviewDialog preview(&p);
    qDebug() << 5;
    connect(&preview, SIGNAL(paintRequested(QPrinter*)), this, SLOT(request(QPrinter*)));
    qDebug() << 6;
    qDebug(&#41; << 7;@

    Return this:

    bq. QWin32PrintEngine::initialize: CreateDC failed ()
    QWin32PrintEngine::initialize: CreateDC failed ()
    Error - RtlWerpReportException failed with status code :-1073741823. Will try to launch the process directly

  • Lifetime Qt Champion

    Then you may have found a bug, you should check the "bug report system":http://bugreports.qt.io to see if it's something known. If not please consider opening a report with a minimal compilable example and as much details as possible about your setup (OS, printer type, driver version etc.)

  • I think documentation is clear that you should not call setPaperSize
    if that size is not supported by printer.
    The result is undefined

  • alex_malyu I am not able to understand what you mean. Could be clearer? You mean the problem is the printer that is causing the "Crash"?

    SGaist: https://bugreports.qt.io/browse/QTBUG-43877

  • Lifetime Qt Champion

    @ alex_malyu I was referring at this "setPageSize":http://doc.qt.io/qt-5/qprinter.html#setPageSize using QPageSize

  • Hi alex_malyu,

    If I using like this:

    @QPrintPreviewDialog preview;
    connect(&preview, SIGNAL(paintRequested(QPrinter*)), this, SLOT(request(QPrinter*)));

    "work", but don't show text in preview and if click in "Page Setup Button" application crash too:

    !http://i57.tinypic.com/16jituf.png(crash qt printer)!

  • Hi Guilherme,

    I'm having a similar, if not the same, problem.

    As of Qt 5.4 there are a lot of functions, arguments, and enums with similar-sounding names. These two functions are obsolete:

    @void QPrinter::​setPageSize(PageSize newPageSize)
    void QPrinter::​setPaperSize(PaperSize newPaperSize)@

    The recommended function to use is:

    @bool QPrinter::​setPageSize(const QPageSize & pageSize)@

    You could use it like this:


    I've been doing it that way and occasionally a customer will have a crash there. The common factor seems to be HP printers. What is your default printer on the computer that's crashing?

  • If you had to read the problem occurs in various situations
    Eg.: if click in “Page Setup Button” application crash too.

    I can say that the problem was with the default printer that was problem in driver (actually the "COM"), deleted the door and the problem stopped. But the point is that it should have a system with "QObjectconnect" to detect the failure, since the windows the "QT" uses "createDC": @https://msdn.microsoft.com/en-us/library/windows/desktop/dd183490(v=vs.85).aspx@

    So the problem is not with no deprecated method (see that in my second post I also used the correct method) but with the method "CreateDC".

    Read this:
    QWin32PrintEngine::initialize: CreateDC failed ()
    QWin32PrintEngine::initialize: CreateDC failed ()@

    These errors appear before calling any method.

  • Just this afternoon I tracked down a similar printing problem that a customer was having. The problem originates in QWin32PrintEnginePrivate::initialize(), the same function that generates the warning message you're getting: "CreateDC failed."

    The problem is that, for some printers, the PRINTER_INFO_2 structure that GetPrinter() returns can have a value of NULL for the pDevMode member. Elsewhere in Qt code it is assumed that pDevMode is not NULL and it is dereferenced causing a crash.

    One of those places is QPrinter::setPageSize(). Another is QPageSetupDialog::exec(). These are two places where your program is crashing.

    Here's a test program you can run (make sure the problem printer is the default printer). This test program uses that same win32 calls that Qt uses but doesn't use Qt for the printing. So you can use this to see if pDevMode is NULL. If it is, then I think my fix will work for both of us.

    @// Win32PrintTest.h

    #ifndef WIN32PRINTTEST_H
    #define WIN32PRINTTEST_H

    #include <QString>
    #include "Windows.h"

    // Run the print test <--- This is the function you should call
    void win32PrintTest();

    // Converts a win32 printer status to a string (PRINTER_STATUS_*)
    QString win32PrinterStatusToString(DWORD status);

    // Converts a win32 paper orientation to a string
    QString win32OrientationToString(short orientation);

    // Converts a win32 paper size to string
    QString win32PaperSizeToString(short paperSize);

    // Converts a win32 print quality to string
    QString win32PrintQualityToString(short printQuality);

    // Converts a win32 duplex option to string
    QString win32DuplexSettingToString(short duplex);

    // Converts a win32 default paper source to string
    QString win32DefaultSourceToString(short source);

    #endif // WIN32PRINTTEST_H

  • Ugh this character limit is killing me. Let me upload these files somewhere.

  • Ok, "here is the header file":http://pastebin.com/57G1kgpF for the test code.

    And "here is the implementation":http://pastebin.com/Cgh1U3tT

  • Lifetime Qt Champion


    Did you took a look at the "bug report system":http://bugreports.qt.io ? There might already be a bug about it and your input would be valuable.

  • Yes I should have checked before I created "this bug report.":https://bugreports.qt.io/browse/QTBUG-44349

    Checking now it look's like there's a few old reports. Is it good etiquette to comment on those like "This is probably related and I think I have a fix."?

  • Lifetime Qt Champion

    If you have a fix and would like to contribute it, just go ahead. Submit your patch to gerrit for code review and include the related bugs in the commit message

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.