Important: Please read the Qt Code of Conduct -

[Solved] QPrinter crash on Qt5

  • Hi, i need help...
    i write small program to print receipt. but having difficulties with Qt5. It always crash. in Qt4 it working just fine. So i decide to test it, here is the code:

    @#include <QCoreApplication>
    #include <QPrinter>
    #include <QPainter>

    int main(int argc, char *argv[])
    QCoreApplication a(argc, argv);

    QPrinter printer;
    QPainter p;
    if (p.begin(&printer))
        p.drawText(0, 0, "test");
    return 0;


    in Qt5 if i replace drawText with drawLine, crash didn't happen. Tested on Qt 5.3.1 MSVC 2013 and Qt 5.2.1 MSVC 2010, Qt 4.8.6 MSVC 2010. What is wrong with my code? Can someone give me a hint about the proper way to use QPrinter on Qt5. I'm just newbie on programming...
    Thank you. Sorry for my bad english.

  • Perhaps you just need a font.

  • thank you for replying..

    @ QFont font("Arial");

    but no luck

  • I tested it: my debugger complained about a missing font and it wanted a QGuiApplication instead of a QCoreApplication. Then it worked on my machine.

  • Lifetime Qt Champion

    Hi and welcome to devnet,

    IIRC you must use a QApplication

  • thank you msue and SGaist. solved...

Log in to reply